如何在 WPF 中更改 DynamicResource

How to change DynamicResource in WPF

我有一个 UserControl XAML:

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

你的情况看起来使用 TriggerDynamicResource 更好,但如果你坚持使用 DynamicResource,你基本上需要定义你的状态' icons/images 作为 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 资源(从后面的代码或从视图模型):

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

更新

如果您想使用 DataTrigger,只需添加一个 属性 以将 connection-status 保存到您的用户控件:

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>