我的数据库中有一个类别表,如下所示。
分类
parentId链接回自身以形成层次结构。
如何将它绑定到WPF中的组合框,以便子元素根据每个级别缩进?
答案 0 :(得分:7)
XAML:
<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
属性