我在数据库中有一个类别表,如下所示。
分类
- categoryId(类别ID)
- name(名称)
- parentId(父级ID)
parentId链接到自身以形成层次结构。
我该如何将其绑定到WPF中的组合框,以便每个级别的子元素都适当缩进?
我在数据库中有一个类别表,如下所示。
分类
parentId链接到自身以形成层次结构。
我该如何将其绑定到WPF中的组合框,以便每个级别的子元素都适当缩进?
<ComboBox ItemsSource="{Binding YourItems}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Margin="{Binding Level, Converter={x:Static my:MainWindow.LevelToMarginConverter}}" Text="{Binding Name}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
C#:
class MainWindow {
......
class LevelToMarginConverterClass : IValueConverter {
const int onelevelmargin = 10;
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
int level = (int)value;
return new Thickness(level * onelevelmargin,0,0,0);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
return null;
}
}
public static IValueConverter LevelToMarginConverter = new LevelToMarginConverterClass();
}
请确保您的类中有 int Level
和 string Name
属性。