作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个带有该 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)。我怎样才能做到这一点?问候
最佳答案
你的情况看起来使用 Trigger
比 DynamicResource
会更好,但如果你坚持使用 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/
我是一名优秀的程序员,十分优秀!