Wpf 设置数据绑定datagrid的行属性
我将DataGrid绑定到一个对象(使用MVVM模式)Wpf 设置数据绑定datagrid的行属性,wpf,datagrid,Wpf,Datagrid,我将DataGrid绑定到一个对象(使用MVVM模式) 在这里: 记录集是一个列表。 这个很好用。数据加载良好,一切正常。我想知道我是否可以在单个行上设置一些属性,即将行属性绑定到ViewModel中的值(根据记录设置每行的row.IsEnable.Enable) 我是WPF的新手 谢谢这应该行得通。将此样式放入数据网格中 <DataGrid.RowStyle> <Style TargetType="DataGrid
在这里:
记录集是一个列表。
这个很好用。数据加载良好,一切正常。我想知道我是否可以在单个行上设置一些属性,即将行属性绑定到ViewModel中的值(根据记录设置每行的row.IsEnable.Enable)
我是WPF的新手
谢谢这应该行得通。将此样式放入数据网格中
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Enabled}" Value="false">
<Setter Property="Visibility" Value="Hidden"/>
<Setter Property="Height" Value="0"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
如果未启用此行的记录绑定,则这将使行不可见且没有高度。“{Binding Path=Enabled}”中的Enabled属性属于您的记录。我不知道你是否已经有了,但你需要类似的东西。跟进wangburger的答案。
要将每个DataGridRow的IsEnabled属性绑定到记录中的Enabled,可以执行以下操作
<DataGrid ItemsSource="{Binding Path=RecordSet}"
AutoGenerateColumns="False"
IsReadOnly="True"
Name="ResultGrid">
<DataGrid.Resources>
<Style TargetType="DataGridRow">
<Setter Property="IsEnabled" Value="{Binding Enabled}"/>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding Path=Id}" Width="Auto"/>
<DataGridTextColumn Header="Foo" Binding="{Binding Path=Foo}" Width="*"/>
</DataGrid.Columns>
</DataGrid>
注意:如果将行样式设置为
IsEnabled=false
,则实际上无法选择该行,光标键也不会跳过该行。这可能不是你想要的
如果您特别想设置
IsEnabled=false
(我知道您只是举了一个例子),但仍然允许选择行,那么您需要(AFAIK)为每个列模板设置IsEnabled=false
。这对我来说似乎有点危险。也许它可以工作(我不是说不会),但我认为行过滤器最好隐藏行,以避免出现任何wierd边缘情况,例如光标键无法按预期工作
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Enabled}" Value="false">
<Setter Property="Visibility" Value="Hidden"/>
<Setter Property="Height" Value="0"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
<DataGrid ItemsSource="{Binding Path=RecordSet}"
AutoGenerateColumns="False"
IsReadOnly="True"
Name="ResultGrid">
<DataGrid.Resources>
<Style TargetType="DataGridRow">
<Setter Property="IsEnabled" Value="{Binding Enabled}"/>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding Path=Id}" Width="Auto"/>
<DataGridTextColumn Header="Foo" Binding="{Binding Path=Foo}" Width="*"/>
</DataGrid.Columns>
</DataGrid>