我有一个包含多列的Datagrid,它有itemsource = {Binding},在后面的代码中我将DataContext设置为ObservableCollection
我的customClass具有标题,类型,语言,类型1(CustomClass),type2(CustomClass)等属性
现在我的问题是在一个列类型中我有文字值说'A'或者' B'现在,如果值为A,那么我的customClass的对象将具有type1的对象,并且type2被设置为null,并且如果type具有值' B'然后将type1设置为null,type2具有值。
现在用语言我希望以这种方式绑定如果type有A然后取值type1.someProperty如果它有B然后取值type2.someProperty。
这是我在xaml中的内容
<DataGrid x:Name="dg_Content" Margin="0,60,-0.4,0" VerticalAlignment="Top" Height="505" ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Auto" ItemsSource="{Binding}" AutoGenerateColumns="False" ColumnWidth="*">
<DataGrid.Columns>
<DataGridTextColumn Header="Title" Binding="{Binding _title}"/>
<DataGridTextColumn x:Name="dgType" Header="Content Type" Binding="{Binding _type}"/>
<DataGridTextColumn Header="Language">
<DataGridTextColumn.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Binding="{Binding ElementName=dgType, Path=Content}" Value="A" />
</MultiTrigger.Conditions>
<Setter Property="Content" Value="{Binding type1.someProperty}" />
</MultiTrigger>
</Style.Triggers>
</Style></DataGridTextColumn.CellStyle>
</DataGridTextColumn>
<DataGridTextColumn Header="Created At" Binding="{Binding _createdAt}"/>
<DataGridTemplateColumn Header="Edit/View">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Edit/View" Click="View_Click"></Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
有人可以帮助我吗?
答案 0 :(得分:0)
如果有人需要,我就是这样做的。
Xaml代码:
<DataGrid x:Name="DgContent" Margin="0,60,-0.4,0" VerticalAlignment="Top" Height="505" ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Auto" CanUserAddRows="False" ItemsSource="{Binding}" AutoGenerateColumns="False" ColumnWidth="*">
<DataGrid.Columns>
<DataGridTextColumn Header="Title" Binding="{Binding Title}"/>
<DataGridTextColumn Header="Content Type" Binding="{Binding Type}"/>
<DataGridTextColumn Header="Language">
<DataGridTextColumn.Binding>
<MultiBinding Converter="{StaticResource ResourceKey=Content}">
<Binding Path="{x:Static local:MainWindow.cl}"></Binding>
</MultiBinding>
</DataGridTextColumn.Binding>
</DataGridTextColumn>
<DataGridTextColumn Header="Created At" Binding="{Binding CreatedAt}"/>
<DataGridTemplateColumn Header="Edit/View">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Edit/View" Click="View_Click"></Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
代码背后:
public object Convert(object[] values, Type targetType,
object parameter, System.Globalization.CultureInfo culture)
{
try
{
if (((Content)values[0]).Type == "movie")
{
return ((Content)values[0]).Movie.Language;
}
else
return ((Content)values[0]).Video.Language;
}
catch (Exception e)
{ return "EXception"; }
}
public object[] ConvertBack(object value, Type[] targetTypes,
object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException("Cannot convert back");
}