Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,703 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I have the following xaml code.
I am trying to bind custom class UnitCategory to combo box.
But seems to be there is an issue in code.
<Grid>
<Grid.Resources>
<local:ComboSourceConverter x:Key="cscconv" />
<Style x:Key="UnitCategoryStyle" TargetType="{x:Type igEditors:XamComboEditor}">
<Setter Property="ItemsSource" Value="{Binding Path=DataContext.UnitCategories, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" />
<Setter Property="SelectedItem" Value ="{Binding Path=DataItem.UnitCategory}" />
<Setter Property="DisplayMemberPath" Value="Name" />
</Style>
<Style x:Key="UnitStyle" TargetType="{x:Type igEditors:XamComboEditor }">
<Setter Property="ItemsSource" Value="{Binding Path=DataItem.UnitCategory, Converter={StaticResource cscconv}}" />
</Style>
</Grid.Resources>
<igWPF:XamDataGrid DataSource="{Binding data}" CellChanged="XamDataGrid_CellChanged">
<igWPF:XamDataGrid.FieldLayoutSettings>
<igWPF:FieldLayoutSettings AutoGenerateFields="False" />
</igWPF:XamDataGrid.FieldLayoutSettings>
<igWPF:XamDataGrid.FieldLayouts>
<igWPF:FieldLayout>
<igWPF:FieldLayout.Fields>
<igWPF:Field Name="Item"></igWPF:Field>
<igWPF:Field Name="UnitCategory">
<igWPF:Field.Settings>
<igWPF:FieldSettings EditorStyle="{StaticResource UnitCategoryStyle}" />
</igWPF:Field.Settings>
</igWPF:Field>
<igWPF:Field Name="Unit">
<igWPF:Field.Settings>
<igWPF:FieldSettings EditorStyle="{StaticResource UnitStyle}" />
</igWPF:Field.Settings>
</igWPF:Field>
</igWPF:FieldLayout.Fields>
</igWPF:FieldLayout>
</igWPF:XamDataGrid.FieldLayouts>
</igWPF:XamDataGrid>
</Grid>
Following view model. Here Unit category is not binding. Drop down in combo box is not showing. Is there any binding issue?
public class ViewModel : INotifyPropertyChanged
{
public ObservableCollection<DataModel> data { get; set; }
public List<UnitCategory> UnitCategories { get; set; }
public static List<string> WeightUnits { get; set; }
public static List<string> DistanceUnits { get; set; }
public ViewModel()
{
this.data = new ObservableCollection<DataModel>();
this.UnitCategories = new List<UnitCategory>();
UnitCategory u1 = new UnitCategory();
u1.Name = "Wt";
UnitCategory u2 = new UnitCategory();
u2.Name = "Dt";
this.UnitCategories.Add(u1);
this.UnitCategories.Add(u2);
WeightUnits = new List<string>()
{
"mg",
"g",
"kg"
};
DistanceUnits = new List<string>()
{
"cm",
"m",
"km"
};
DataModel data1 = new DataModel();
data1.Item = "Item 1";
DataModel data2 = new DataModel();
data2.Item = "Item 2";
this.data.Add(data1);
this.data.Add(data2);
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public class DataModel
{
public string Item { get; set; }
public UnitCategory UnitCategory { get; set; }
public string Unit { get; set; }
public DataModel()
{
}
}
public class UnitCategory
{
public string Name { get; set; }
}