我正在尝试用一列作为组合框填充数据网格,但我需要当绑定到组合框的集合为空时,该列成为文本框列。我已将列定义如下:
绑定绑定 = new Binding("DataContext.Prices"); binding.RelativeSource = new RelativeSource(RelativeSurceMode.FindAncestor, typeof(UserControl),1);
DataGridComboBoxColumn productPrices = new DataGridComboBoxColumn()
{
ElementSyle = new Style
{
TargetType = typeof(ComboBox),
Setters =
{
new Setter
{
Property=ComboBox.ItemsSourceProperty,
Value= binding
}
}
},
EditingElementSyle = new Style
{
TargetType = typeof(ComboBox),
Setters =
{
new Setter
{
Property=ComboBox.ItemsSourceProperty,
Value= binding
}
}
},
DisplayMemberPath = new Binding("Price");
SelectedValuePath = new Bindnt("Price");
};
myDataGrid.Columns.Add(productPrices);
myDataGrid.Columns.Add(new DataGridTextColumn(){ Header="Name", Binding=new Binding("Name")});
我定义了 myDataGrid:
<DataGrid Name="myDataGrid" ItemsSource="{Binding Products}" />
在我的视图模型中,我创建了一个
var products = new List<Product>
{
new Product
{
Name="Prod 1",
Price="12.5"
}
}
var prices = new List<PriceL>
{
new PriceL
{
Price="12.5"
},
new PriceL
{
Price="10"
}
}
ICollectionView Products = CollectionViewSource.GetDefaultView(products);
ICollectionView Prices = CollectionViewSource.GetDefaultView(prices);
我需要当“价格”为空时,该列变为我正在使用 MVVM 的文本框中,我尝试使用 elementStyle,但我在 Combobox 中看不到任何让我验证它的数据源的事件。任何人都可以帮助我吗?