Following entry does not work:
<TextBox Text="{Binding Path=Name}"/>
I am unable to create a simple binding to the MyItems-Class within the trigger. Outside the trigger it works. Do I need to enter any additional binding information there?
Can someone help me?
public class ViewModel: INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public ObservableCollection<MyItem> MyItems {
get;
set;
}
public ViewModel()
{
MyItems = new ObservableCollection<MyItem>() {
new MyItem() { Name="1", ControlType="Button" },
new MyItem() { Name="2", ControlType="CheckBox" },
new MyItem() { Name="3", ControlType="TextBox" }
};
}
}
public class MyItem : INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private string name;
public string Name {
get {
return name;
}
set {
if (value != name) {
name = value;
OnPropertyChanged("Name");
}
}
}
private string controlType;
public string ControlType { get => controlType; set => controlType = value; }
}
<DataGrid ItemsSource="{Binding MyItems}"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Control Type" Binding="{Binding ControlType}"/>
<DataGridTemplateColumn Header="Actual Control">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ContentControl>
<ContentControl.Style>
<Style TargetType="ContentControl">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=ControlType}" Value="TextBox">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBox Text="Test"/>
<!-- This doesn't work <TextBox Text="{Binding Path=Name}"/>-->
</DataTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Try to bind the
Contentproperty to the data object: