Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set DataContext in XAML

Tags:

c#

wpf

xaml

I have this simple application that adds some items to a combobox:

public partial class Window1 : Window
    {
        private ObservableCollection<string> _dropDownValues = new ObservableCollection<string>();
        public ObservableCollection<string> DropDownValues
        {
            get { return _dropDownValues; }
            set { _dropDownValues = value; }
        }

        private string _selectedValue;
        public string SelectedValue
        {
            get { return _selectedValue; }
            set { _selectedValue = value; }
        }

        public Window1()
        {
            InitializeComponent();
            DataContext = this;

            DropDownValues.Add("item1");
            DropDownValues.Add("item1");
            DropDownValues.Add("item1");
            DropDownValues.Add("item1");
            DropDownValues.Add("item1");
            DropDownValues.Add("item1");
        }
    }

And here is the XAML file:

<Window x:Class="WpfApplication2.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel HorizontalAlignment="Left" Margin="10">
        <ComboBox
            Margin="0 0 0 5"
            ItemsSource="{Binding DropDownValues}"
            SelectedValue="{Binding SelectedValue}"        
            Width="150"/>     
    </StackPanel>
</Window>

Can someone show me how can I set the DataContext from the xaml file instead of initializing in the constructor ?

Thanks.

like image 946
Adrian Avatar asked Aug 25 '11 14:08

Adrian


People also ask

What is DataContext in XAML?

The DataContext property is the default source of your bindings, unless you specifically declare another source, like we did in the previous chapter with the ElementName property. It's defined on the FrameworkElement class, which most UI controls, including the WPF Window, inherits from.

What is UserControl DataContext?

DataContext is inherited to all lower Elements of the XAML and to all the XAML of UserControl s unless it is overwritten somewhere. By setting the UserControl DataContext to itself, this overwrites the DataContext and breaks Inheritance. Instead, nest it one Element deep in the XAML, in your case, the StackPanel .

What is DataContext?

The DataContext is the source of all entities mapped over a database connection. It tracks changes that you made to all retrieved entities and maintains an "identity cache" that guarantees that entities retrieved more than one time are represented by using the same object instance.


1 Answers

Just change the Window to bind DataContext to itself:

<Window x:Class="WpfApplication2.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300"
        DataContext="{Binding RelativeSource={RelativeSource Self}}" ... />
like image 124
Rhyous Avatar answered Oct 21 '22 12:10

Rhyous