我试图将一个Combobox绑定到一个数据网格中的对象,但不太成功。
我想把Combobox绑定到数据网格中的一个对象上,但不太成功。
public class Object {
public static IEnumerable<string> Colors => new List<string> {"Red", "Green", "Blue"}
}
public string Color {
get => color;
set => color = value;
}
private string Color;
我的ViewModel中包含了这些模型的集合,我把它们叫做 Objects
.
XAML文件。
<DataGrid Name="DataGrid" ItemsSource={Binding Objects}" Style={StaticResource DataGridStyle}">
<DataGrid.Columns>
<DataGridComboBoxColumn Header="Test" SelectedValueBinding="{Binding Color, StringFormat=F3, Mode=TwoWay}" ItemsSource="{Binding Colors}"/>
<DataGrid.Columns/>
<DataGrid/>
谁知道我做错了什么?现在,我所看到的是一个空白的空间,那里应该是组合框。我尝试了以下的方法,但没有成功,我怀疑可能是问题所在。
Color
的属性是在对象本身还是在我的ViewModel中?我两者都试过了,我也试过把这个属性变成静态的。DataGridComboBoxColumn.ElementStyle
和 DataGridComboBoxColumn.EditingElementStyle
这应该可以。
<DataGridComboBoxColumn Header="Test"
SelectedValueBinding="{Binding Color, StringFormat=F3, Mode=TwoWay}"
ItemsSource="{x:Static local:Object.Colors}"/>
local
映射到名字空间里 Object
被定义为。
xmlns:local="clr-namespace:WpfApp1"