拥有此型号:
public class OptimizationSetting
{
public bool Selected { get; set; }
public string SettingName { get; set; }
public bool HasValue { get; set; }
public double Value { get; set; }
}
这个 xaml 页面:
<DataGrid BorderBrush="{x:Null}" ItemsSource="{Binding OptimizationSettings}" Margin="1" IsReadOnly="False" Background="White"
AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False" RowHeaderWidth="20"
CanUserResizeColumns="False" CanUserResizeRows="False">
<DataGrid.Columns>
<DataGridCheckBoxColumn Header="Selected" Binding="{Binding Selected}" Width="80"/>
<DataGridTextColumn Header="Setting" Binding="{Binding SettingName}" Width="*"/>
<DataGridTextColumn Header="Value" Binding="{Binding Value}" Width="*"/>
</DataGrid.Columns>
</DataGrid>
我试图根据模型 HasValue 值隐藏 DataGrid Value 内容。我可以通过使用样式来实现这一点吗?
您可以找到多种方法来隐藏单元格的内容。以下是一个例子。
<DataGridTextColumn Header="Value" Binding="{Binding Value}" Width="*">
<DataGridTextColumn.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<DataTrigger Binding="{Binding HasValue, Mode=OneTime}" Value="False">
<Setter Property="Visibility" Value="Hidden"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>