更新具有无限可能条件的wpf样式

发布于 2024-11-02 12:35:14 字数 2969 浏览 1 评论 0原文

我有一个 DataGrid,其中的行代表我正在执行 ping 操作的主机,还有一个名为 Lost 的列,该列代表丢失的 ICMP 数据包,随着时间的推移,其值会增加。我已经把整个 INotifyPropertyChanged 事情搞定了,而且我看到了价值的增加。我想要做的是编写一个 Style ,它将相对于 Lost 列的值逐渐将行的背景颜色从白色更改为深红色。

如果可能的话,我想编写一个 TriggerDataTrigger,并将 setter 值设置为 ValueConverter 来计算所需的颜色,但到目前为止,我还没有成功编写一种每当丢失单元格的值发生变化时都会更新的样式。当我加载新的数据上下文并切换回来(仅用于测试)时,我只看到颜色的差异。

这是我尝试过的:

    <Style x:Key="DownStyle" TargetType="{x:Type DataGridRow}">
        <Setter Property="Background" Value="{Binding Converter={StaticResource BackgroundConverter}}" />
    </Style>

我看不到它与 DataTrigger 一起工作,因为无论如何你都必须指定一个值,并且这些值是无限的(或者我猜是 Int32.MaxValue)。尽管我也尝试为 value 属性指定一个 ValueConverter ,但这也不起作用。

顺便说一句,我想尽可能避免代码隐藏。

编辑: Rick:我曾尝试做类似的事情:

    <Style x:Key="DownStyle" TargetType="{x:Type DataGridRow}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Converter={StaticResource LostColumnValueConverter}}" Value="Somenumber">
                <Setter Property="Background" Value="{Binding Converter={StaticResource BackgroundConverter}}" />
            </DataTrigger>
        </Style.Triggers>
    </Style>

我想我理解需要将 Trigger 实际绑定到将要更改的内容(在这种情况下,我觉得我被迫也使用 < code>ValueConverter 来获取列的值,有没有更直接的方法?),但即使我这样做,我应该指定什么作为 DataTrigger 的值?

编辑: 因此,就我而言,我继续执行以下操作(目前这只会修改 TextBlock 的背景):

<DataGridTemplateColumn Header="Lost" Width="Auto">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Lost, NotifyOnTargetUpdated=True}">
                <TextBlock.Triggers>
                    <EventTrigger RoutedEvent="Binding.TargetUpdated">
                        <BeginStoryboard>
                            <Storyboard>
                                <ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetProperty="Background">
                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{Binding Converter={StaticResource BackgroundConverter}}" />
                                </ObjectAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                </TextBlock.Triggers>
            </TextBlock>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

在我看来,这似乎是正确的,但由于某种原因只能运行一次。我在 TextBlock 定义上添加了一个单独的 TargetUpdated 事件处理程序,以查看该事件是否确实在每次更改时被调用,事实确实如此。 我的 EventTrigger 上肯定缺少某些内容。可能与故事板有关。 不管怎样,对于如此简单的事情来说,这一切似乎都非常冗长,所以我继续采用代码隐藏路线。

I have a DataGrid with rows representing a host I'm doing pings to and a column called Lost which represents lost ICMP packets which over time increases in value. I have the whole INotifyPropertyChanged thing down and I'm seeing value increase. What I want to do is write a Style that'll change a row's background color from white to dark red progressively relative to the Lost column's value.

I would like, if it were possible, to write a Trigger or DataTrigger with a setter value set to a ValueConverter which would calculate the color needed, but so far I've been unsuccessful in writing a style that will update every time a Lost cell's value changes. I only see a difference in color when I load a new data context and switch back (just for testing).

Here's what I've tried:

    <Style x:Key="DownStyle" TargetType="{x:Type DataGridRow}">
        <Setter Property="Background" Value="{Binding Converter={StaticResource BackgroundConverter}}" />
    </Style>

I can't see this working with a DataTrigger since you have to specify a value anyways and the values are infinite (or Int32.MaxValue I guess), really. Although I also tried specifying a ValueConverter for the value property and that didn't work either.

Btw, I want to try to avoid code-behind if possible.

Edit:
Rick: I had tried doing something like:

    <Style x:Key="DownStyle" TargetType="{x:Type DataGridRow}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Converter={StaticResource LostColumnValueConverter}}" Value="Somenumber">
                <Setter Property="Background" Value="{Binding Converter={StaticResource BackgroundConverter}}" />
            </DataTrigger>
        </Style.Triggers>
    </Style>

I think I understand the need to have the Trigger actually bind to something that's going to be changing (in this case I feel I'm forced to also use a ValueConverter to get the column's value, is there a more direct way?), but even if I do this, what do I specify as the DataTrigger's value?

Edit:
So in my case I went ahead and did the following (currently this would only modify the TextBlock's background):

<DataGridTemplateColumn Header="Lost" Width="Auto">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Lost, NotifyOnTargetUpdated=True}">
                <TextBlock.Triggers>
                    <EventTrigger RoutedEvent="Binding.TargetUpdated">
                        <BeginStoryboard>
                            <Storyboard>
                                <ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetProperty="Background">
                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{Binding Converter={StaticResource BackgroundConverter}}" />
                                </ObjectAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                </TextBlock.Triggers>
            </TextBlock>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

Which to me seems right, but only works once for some reason. I added a seperate TargetUpdated event handler right on the TextBlock definition to see if the event was indeed being called on every change and it is.
Something must be missing on my EventTrigger. Probably something to do with the Storyboard.
Anyways, this all seems incredibly verbose for something so simple, so I went ahead and went with the code-behind route.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仅此而已 2024-11-09 12:35:14

我认为 Triggers 和 DataTriggers 在这种情况下无济于事,因为 datagrigger 的值不稳定。更糟糕的是,它不是依赖属性,您无法绑定到它。在我看来,更好的方法是使用 EventTrigger。

    <Window.Resources>
    <BeginStoryboard x:Key="bsbPing">
        <Storyboard>
            <DoubleAnimation Duration="0:0:0.2" Storyboard.TargetProperty="FontSize" To="{Binding Path=PingValue}"  />
        </Storyboard>
    </BeginStoryboard>
</Window.Resources>

<Grid>
    <StackPanel>
        <TextBox Name="txbPingValue" Text="{Binding Path=PingValue}">
            <TextBox.Triggers>
                <EventTrigger RoutedEvent="TextBox.TextChanged">
                    <EventTrigger.Actions>
                        <StaticResource ResourceKey="bsbPing" />
                    </EventTrigger.Actions>
                </EventTrigger>
            </TextBox.Triggers>
        </TextBox>
        <Button Name="btnPing" Click="btnPing_Click">Ping</Button>
    </StackPanel>
</Grid>

和代码:

    public partial class Window7 : Window
{
    public Ping MyPing { get; set; }

    public Window7()
    {
        InitializeComponent();

        MyPing = new Ping { PingValue = 20.0 };
        this.DataContext = MyPing;

    }

    private void btnPing_Click(object sender, RoutedEventArgs e)
    {
        MyPing.PingValue += 10;
    }
}

public class Ping : INotifyPropertyChanged
{
    private double pingValue;
    public double PingValue 
    {
        get
        {
            return pingValue;
        }
        set
        {
            pingValue = value;
            NotifyPropertyChanged("PingValue");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }

}

I suppose Triggers and DataTriggers can't help in this case because datagrigger's value is unstable. And worse, it's not dependency property and you can't bind to it. It seems to me the better way is to use EventTrigger.

    <Window.Resources>
    <BeginStoryboard x:Key="bsbPing">
        <Storyboard>
            <DoubleAnimation Duration="0:0:0.2" Storyboard.TargetProperty="FontSize" To="{Binding Path=PingValue}"  />
        </Storyboard>
    </BeginStoryboard>
</Window.Resources>

<Grid>
    <StackPanel>
        <TextBox Name="txbPingValue" Text="{Binding Path=PingValue}">
            <TextBox.Triggers>
                <EventTrigger RoutedEvent="TextBox.TextChanged">
                    <EventTrigger.Actions>
                        <StaticResource ResourceKey="bsbPing" />
                    </EventTrigger.Actions>
                </EventTrigger>
            </TextBox.Triggers>
        </TextBox>
        <Button Name="btnPing" Click="btnPing_Click">Ping</Button>
    </StackPanel>
</Grid>

and code:

    public partial class Window7 : Window
{
    public Ping MyPing { get; set; }

    public Window7()
    {
        InitializeComponent();

        MyPing = new Ping { PingValue = 20.0 };
        this.DataContext = MyPing;

    }

    private void btnPing_Click(object sender, RoutedEventArgs e)
    {
        MyPing.PingValue += 10;
    }
}

public class Ping : INotifyPropertyChanged
{
    private double pingValue;
    public double PingValue 
    {
        get
        {
            return pingValue;
        }
        set
        {
            pingValue = value;
            NotifyPropertyChanged("PingValue");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }

}
遗弃M 2024-11-09 12:35:14

您使用转换器的方向是正确的。您使用的技术有时称为“分箱”,它将一大组整数映射到一组固定的小值。它还用于创建直方图。

只要不更新,您的绑定表达式必须更加具体才能检索 Lost 属性,否则当 Lost 更改时不会重新评估它。更改后,您还需要更新转换器。

You are on the right track with the converter. The technique you are using is sometimes called "binning" which maps a large set of integers to a fixed small set of values. It is also used to create histograms.

As far as not updating, your binding expression has to be more specific to retrieve the Lost property, otherwise it won't be re-evaluated when Lost changes. Once you change it you'll also need to update your converter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文