将 Datagrid 与 observable collection wpf 绑定
Binding Datagrid with observable collection wpf
我有一个包含多个列的 Datagrid,它有 itemsource={Binding} 并且在后面的代码中我将 DataContext 设置为一个 ObservableCollection
我的 customClass 具有标题、类型、语言、type1(CustomClass)、type2(CustomClass) 等属性
现在我的问题是,在一列类型中我有文本值说 'A' 或 'B' 现在如果值为 A 那么我的 customClass 的 object 将有一个 object type1 和 type2 设置为 null,如果类型具有值 'B',则 type1 设置为 null 并且 type2 具有值。
现在在语言中我想以这种方式绑定如果类型有 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>
有人能帮帮我吗?
如果有人需要的话,我是这样做的。
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");
}
我有一个包含多个列的 Datagrid,它有 itemsource={Binding} 并且在后面的代码中我将 DataContext 设置为一个 ObservableCollection
我的 customClass 具有标题、类型、语言、type1(CustomClass)、type2(CustomClass) 等属性
现在我的问题是,在一列类型中我有文本值说 'A' 或 'B' 现在如果值为 A 那么我的 customClass 的 object 将有一个 object type1 和 type2 设置为 null,如果类型具有值 'B',则 type1 设置为 null 并且 type2 具有值。
现在在语言中我想以这种方式绑定如果类型有 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>
有人能帮帮我吗?
如果有人需要的话,我是这样做的。
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");
}