【问题标题】:How to change DynamicResource in WPF如何在 WPF 中更改 DynamicResource
【发布时间】:2018-07-31 11:56:04
【问题描述】:

我有一个带有该 XAML 的 UserControl:

<StackPanel>
    <Label HorizontalContentAlignment="Center">
        <Rectangle Name="iconContainer" Height="120" Width="120" Fill="#FF045189">
            <Rectangle.OpacityMask>
                <VisualBrush Visual="{DynamicResource appbar_disconnect}"/>
            </Rectangle.OpacityMask>
        </Rectangle>
    </Label>
    <TextBlock Name="tBlockPortStatus" Foreground="#FF7C7676" FontWeight="Bold" FontSize="15" Margin="3" TextAlignment="Center" HorizontalAlignment="Center" TextWrapping="Wrap">PORT STATUS (Testing wrapping text abilities for this control)</TextBlock>
</StackPanel>

我需要更改图标(名为 appbar_disconnect)并使用 Code Behind 或 MVVM 使用另一个 DynamicResource(例如 appbar_connect)。 我怎样才能做到这一点? 问候

【问题讨论】:

  • 根据什么改变图标?
  • 基于特定的事件触发。但我的问题是如何在运行时更改此 DynamicResource 以使用其他图标。此 UserControl 将使用图标和状态消息向用户显示应用程序状态。

标签: wpf xaml mvvm code-behind dynamicresource


【解决方案1】:

您的情况看起来使用TriggerDynamicResource 处理得更好,但是如果您坚持使用DynamicResource,您基本上需要将您所在州的图标/图像定义为应用程序中的资源。 xml 文件:

 <Application.Resources>
    <Image Source="Icons/disconnect.png" x:Key="AppbarDisconnect"/>
    <Image Source="Icons/connect.png" x:Key="AppbarConnect"/>
    <Image Source="Icons/undefined.png" x:Key="AppbarStatus"/>
</Application.Resources>

假设您的 UserControl 看起来像这样:

<StackPanel>
    <Label HorizontalContentAlignment="Center">
        <Rectangle Name="IconContainer" Height="120" Width="120" Fill="#FF045189">
            <Rectangle.OpacityMask>
                <VisualBrush Visual="{DynamicResource AppbarStatus}"/>
            </Rectangle.OpacityMask>
        </Rectangle>
    </Label>
    <TextBlock Name="TBlockPortStatus" Foreground="#FF7C7676" FontWeight="Bold" FontSize="15" Margin="3" TextAlignment="Center" HorizontalAlignment="Center" TextWrapping="Wrap">PORT STATUS (Testing wrapping text abilities for this control)</TextBlock>
</StackPanel>

您可以像这样更新特定事件的AppbarStatus 资源(从背后的代码或从视图模型):

Application.Current.Resources["AppbarStatus"] = Application.Current.Resources["AppbarDisconnect"];

更新

如果您想使用DataTrigger,只需添加一个属性来保存用户控件的连接状态:

private bool _connetionStatus;
    public bool ConnectionStatus
    {
        get { return _connetionStatus; }
        set
        {
            if (value == _connetionStatus) return;
            _connetionStatus = value;
            OnPropertyChanged();
        }
    }

DataTrigger 应该是不言自明的:

 <StackPanel>
        <Label HorizontalContentAlignment="Center">
            <Rectangle Name="IconContainer" Height="120" Width="120" Fill="#FF045189">
                <Rectangle.Style>
                    <Style TargetType="Rectangle">
                        <Setter Property="OpacityMask">
                            <Setter.Value>
                                <VisualBrush Visual="{DynamicResource AppbarDisconnect}"/>
                            </Setter.Value>
                        </Setter>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding ConnectionStatus}" Value="true">
                                <Setter Property="OpacityMask">
                                    <Setter.Value>
                                        <VisualBrush Visual="{DynamicResource AppbarConnect}"/>
                                    </Setter.Value>
                                </Setter>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Rectangle.Style>

            </Rectangle>
        </Label>
        <TextBlock Name="TBlockPortStatus" Foreground="#FF7C7676" FontWeight="Bold" FontSize="15" Margin="3" TextAlignment="Center" HorizontalAlignment="Center" TextWrapping="Wrap">PORT STATUS (Testing wrapping text abilities for this control)</TextBlock>
    </StackPanel>

【讨论】:

  • 我是 WPF 新手,所以我想知道如何在特定问题中使用触发器。
  • 太棒了!!!谢谢!!但是这段代码会将图标更改为我所有的图块,好吧,让我更清楚一点,我正在构建和应用程序来控制 comports 并让用户了解实时发生的事情,所以我会有多个那些同时显示不同 COMPORT 状态的 UC。我需要能够根据当前端口状态更改每个 UC 的图标。
  • 正如我所说的 DataTrigger 或简单的 Trigger 看起来更适合这种情况,只需将属性(枚举或布尔值)附加到每个 UC,并使用更新 VisualBrush相应地触发。查看我的更新答案。
  • 太棒了!!!按照这个想法,我还可以添加更多状态(例如阅读、写作、与端口通信时出错、打印等)谢谢!
  • .Resources 不是 Application 独有的,因此您可以在 YourPage.xaml 中将其设置为 ContentPage.Resources 例如,并使用与此答案中几乎相同的代码(更新前)。跨度>
猜你喜欢
  • 1970-01-01
  • 2012-01-19
  • 2010-12-16
  • 1970-01-01
  • 2011-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多