gpt4 book ai didi

DataGrid 的 WPF Datatrigger 仅更改整行

转载 作者:行者123 更新时间:2023-12-04 00:48:22 26 4
gpt4 key购买 nike

我有一个带有 DataTrigger 设置的 DataGrid,所以当它的值为“UP”时,我希望触发器仅更改“directionColumn”datagridcolumn 中的背景。

到目前为止,我得到的更新了整行。我还尝试将“targetname”放在 setter 元素中,但这只是给出了一个错误。请帮忙。

XAML:

<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding}" Name="summaryReportDataGridDATE" RowDetailsVisibilityMode="VisibleWhenSelected" Width="330">
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Direction}" Value="UP">
<Setter>
<Setter.Property>Background</Setter.Property>
<Setter.Value>Red</Setter.Value>
<!--<Setter Property="Background" Value="Red" TargetName="directionColumn"/>-->
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
<DataGrid.Columns>
<!--<DataGridTemplateColumn x:Name="dateColumn" Header="Date" Width="100" CanUserReorder="False" CanUserSort="False" FontFamily="Arial Rounded MT" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DatePicker SelectedDate="{Binding Path=Date, Mode=OneWay}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>-->

<DataGridTextColumn x:Name="dateColumn" Binding="{Binding Path=Date, StringFormat={}{0:dd/MM/yyyy}}" Header="Date" Width="80" CanUserReorder="False" CanUserSort="False" FontFamily="Arial Rounded MT"/>
<DataGridTextColumn x:Name="closeColumn" Binding="{Binding Path=ClosingPrice}" Header="ClosingPrice" Width="80" CanUserReorder="False" CanUserSort="False" FontFamily="Arial Rounded MT" />
<DataGridTextColumn x:Name="directionColumn" Binding="{Binding Path=Direction}" Header="Direction" Width="80" CanUserReorder="False" CanUserSort="False" FontFamily="Arial Rounded MT" />
<DataGridTextColumn x:Name="resultColumn" Binding="{Binding Path=Result}" Header="Result" Width="80" CanUserReorder="False" CanUserSort="False" FontFamily="Arial Rounded MT" />
</DataGrid.Columns>
</DataGrid>

最佳答案

用 TemplateColumn 代替自定义单元格样式怎么样:

    <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding}" Name="summaryReportDataGridDATE" RowDetailsVisibilityMode="VisibleWhenSelected" Width="330">                       
<DataGrid.Columns>
<DataGridTextColumn x:Name="dateColumn" Binding="{Binding Path=Date, StringFormat={}{0:dd/MM/yyyy}}" Header="Date" Width="80" CanUserReorder="False" CanUserSort="False" FontFamily="Arial Rounded MT"/>
<DataGridTextColumn x:Name="closeColumn" Binding="{Binding Path=ClosingPrice}" Header="ClosingPrice" Width="80" CanUserReorder="False" CanUserSort="False" FontFamily="Arial Rounded MT" />
<DataGridTemplateColumn x:Name="directionColumn" Header="Direction" Width="80" CanUserReorder="False" CanUserSort="False" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock x:Name="directionColumnTextBlock" Text="{Binding Path=Direction}" Width="80" FontFamily="Arial Rounded MT"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=Direction}" Value="UP">
<Setter TargetName="directionColumnTextBlock" Property="Background" Value="Red" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn x:Name="resultColumn" Binding="{Binding Path=Result}" Header="Result" Width="80" CanUserReorder="False" CanUserSort="False" FontFamily="Arial Rounded MT" />
</DataGrid.Columns>
</DataGrid>

关于DataGrid 的 WPF Datatrigger 仅更改整行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3310097/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com