Anchit Pancholi
Anchit Pancholi

Reputation: 1214

WPF ComboBox Set using Grid DataContext

I am new to C# WPF, I am tring to set DataContext to combobox my xml is a below

<Grid Name="groupEditArea"  HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="#FFD8D8D8" Margin="-14,0,192,0">
                    <Label Content="Group Name" FontSize="18" HorizontalAlignment="Left" Margin="116,50,0,0" VerticalAlignment="Top" Width="136"/>
                    <Label Content="Group Type" FontSize="18" HorizontalAlignment="Left" Margin="116,123,0,0" VerticalAlignment="Top" Width="136"/>
                    <TextBox x:Name="groupGroupNameTxt" HorizontalAlignment="Left" FontSize="16" Height="31" Margin="368,50,0,0" TextWrapping="Wrap" Text="{Binding Path = GroupName, Mode=TwoWay, StringFormat=\{0:n3\}, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="226"  TextChanged="groupGroupNameTxt_TextChanged"  /> <!-- GotFocus="GroupGroupNameTxt_OnGotFocus" TextInput="GroupGroupNameTxt_OnTextInput"  -->
                    <ComboBox x:Name="groupGroupNameCombo" HorizontalAlignment="Left" Margin="368,123,0,0" VerticalAlignment="Top" Width="226" Height="31" SelectionChanged="groupGroupNameCombo_SelectionChanged"  DisplayMemberPath="GroupName" SelectedValuePath="CategoriesVal" SelectedValue="{Binding Categories}"/>
                </Grid>

my POCO as below :-

        public class Test: INotifyPropertyChanged
        {
            public Test()
            {
            }
     public virtual string TestId { get; set; }
            public virtual Categories CategoriesVal { get; set; }
     public virtual string Name{ get; set; }

    public virtual string GroupName
            {
                get { return Name; }
                set
                {
                    Name = value;
                    OnPropertyChanged("GroupName");
                }
            }
    }

public class Categories : INotifyPropertyChanged
        {
            public Categories ()
            {
            }
 public virtual string CategorieId { get; set; }
     public virtual string Name{ get; set; }

    public virtual string GroupName
            {
                get { return Name; }
                set
                {
                    Name = value;
                    OnPropertyChanged("GroupName");
                }
            }
    }
}

and in my backend code i am setting DataContext as below :-

Categories cate = new Categories ();
cate.CategorieId = "cate1ID";
cate.GroupName = "CateGroupName1"
Test test = new Test();
test.TestId = "TestID";
test.CategoriesVal  = cate;
test.Name = "TestName1";

and groupGroupNameCombo is set using ItemsSource and that contain entire list on Categories when i set using below its working fine

groupGroupNameCombo.SelectedItem = cate;

but when i set using below to grid it wont work :-

groupEditArea.DataContext = test;

can someone guide me how can i set combobox by setting grid DataContext instead of setting manually combobox.

Upvotes: 1

Views: 126

Answers (1)

dovid
dovid

Reputation: 6481

instead

SelectedValuePath="CategoriesVal" SelectedValue="{Binding Categories}"

write

SelectedItem="{Binding CategoriesVal}"

SelectedValuePath means: the name of property (from the ComboBoxItem DataContex - Categories class In our case) from which the value of SelectedValue will be provided.
This is useful in case you want to representation of a instance-item (each form Combobox.Items) is not done by the item itself, but by one characteristic. In your case I do not see any point in it.

see more: Difference between SelectedItem, SelectedValue and SelectedValuePath

Upvotes: 1

Related Questions