I am trying to populate a datagrid with one column as a combobox but I need that when the collection binded to the combobox is empty the column becomes a textbox column. I have defined the columns as below:
Binding binding = 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")});
And I defined myDataGrid:
<DataGrid Name="myDataGrid" ItemsSource="{Binding Products}" />
In my viewmodel I create a
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);
I need that when “Prices” is empty the column become in a textbox I am working with MVVM and I tried with elementStyle but I cannot see any event in Combobox that let me verify it’s data source. Could any body help me ??
I just found one way to do that
Y por codigo puede cambiarse el content
myContent.Content = this.FindResource(“DataGrid2”);