wpf - 如何在 WPF 中更改 DynamicResource

标签 wpf xaml mvvm code-behind dynamicresource

我有一个带有该 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)并使用另一个使用代码隐藏或 MVVM 的 DynamicResource(例如 appbar_connect)。 我怎样才能做到这一点? 问候

最佳答案

你的情况看起来使用 TriggerDynamicResource 会更好,但如果你坚持使用 DynamicResource,你基本上需要将您的状态的图标/图像定义为 App.xaml 文件中的资源:

 <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 资源(从后面的代码或从 View 模型):

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 - 如何在 WPF 中更改 DynamicResource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48896815/

相关文章:

c# - 如何创建 WPF 验证规则以确保两个文本字段具有相同的值?

DataGrid 的 WPF Datatrigger 仅更改整行

mvvm - 在 xamarin 表单 MVVM 中从服务加载数据的最佳位置

wpf - 正确的 MVVM 模式 WPF 命令实现

c# - 如何在没有模式的情况下将源 MediaCapture 绑定(bind)到 CaptureElement?

c# - 图像编辑异常(exception)

c# - 使用触发器更改已绑定(bind)到 View 模型的属性

c# - WPF - 菜单项缺少图标/图像

wpf - 如何在 WPF 中将多个字段绑定(bind)到一个 TextBlock?

WPF XAML 编辑器总是说具有自定义键类型的字典不是集合