c# - 如何绑定到样式中的静态资源图像?
问题描述
我有一种在复选标记和空格之间切换的样式。我想使用我的 StaticResource 来显示图像,而不是我的图像资源文件的硬编码路径。问题是 Image.Source 不允许我使用静态资源并给出错误“InvalidCastException:无法将'System.Windows.Style'类型的对象转换为'System.Windows.Media.ImageSource'。” 好的,我也尝试将 Image.Source 更改为 Image.Style 但这也给出了另一个错误“ArgumentException:不允许样式对象影响其应用对象的样式属性。” 我无法完全弄清楚这一点。任何帮助,将不胜感激。
我确实确保我的 images.xaml 资源位于 App.xaml 中合并资源列表中的 styles.xaml 资源之上。我相信这很重要。
样式.xaml
<Style x:Key="TMGrayCheckedRadioButton" TargetType="{x:Type RadioButton}" BasedOn="{StaticResource {x:Type ToggleButton}}">
<Setter Property="Background" Value="{StaticResource GrayButtonBackground}"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="Width" Value="{StaticResource BBWidth}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToggleButton">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Border x:Name="border" Background="{TemplateBinding Background}"
Grid.ColumnSpan="3"
BorderBrush="Black" BorderThickness="1"
CornerRadius="4">
</Border>
<ContentPresenter x:Name="contentPresenter"
Grid.Column="1"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
<Border x:Name="checked"
Grid.ColumnSpan="3"
IsHitTestVisible="False"
Background="Transparent"
CornerRadius="4" />
<Image Margin="5" Height="{StaticResource fontSize}">
<Image.Style>
<Style>
<Setter Property="Image.Source" Value="/images/blank.png" />
<!--<Setter Property="Image.Style" Value="{StaticResource Blank}" />-->
<Style.Triggers>
<DataTrigger Binding="{Binding IsChecked,
RelativeSource={RelativeSource AncestorType=
{x:Type ToggleButton}}}" Value="True">
<Setter Property="Image.Source" Value="/images/checked.png" />
<!--<Setter Property="Image.Source" Value="{StaticResource Checked}" />-->
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
<Image Margin="5" Grid.Column="2" Height="{StaticResource fontSize}">
<Image.Style>
<Style>
<Setter Property="Image.Source" Value="/images/blank.png" />
<!--<Setter Property="Image.Source" Value="{StaticResource Blank}" />-->
</Style>
</Image.Style>
</Image>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{StaticResource LightGrayButtonBackground}"/>
</Trigger>
<Trigger Property="IsChecked" Value="True">
<!--<Setter Property="Foreground" Value="White"/>-->
<Setter Property="Background" Value="{StaticResource DarkButtonBackground}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
应用程序.xaml
<Application x:Class="TMUI.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TMUI"
StartupUri="Views/SplashWindowView.xaml">
<!-- <StartupUri="MainWindow.xaml"> -->
<!-- Note: StartupUri is local to App.xaml -->
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- Load Infrastructure's Resource Dictionaries -->
<ResourceDictionary Source="Resources\Fonts.xaml" />
<ResourceDictionary Source="Resources\Images.xaml" />
<ResourceDictionary Source="Resources\Styles.xaml" />
<ResourceDictionary Source="pack://application:,,,/Chart2DControl;component/Resources/Controls.xaml"/>
<ResourceDictionary Source="pack://application:,,,/Chart2DControl;component/Resources/ColorsAndBrushes.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
解决方案
推荐阅读
- mysql - 在 SQL 中进行排名时,“+”号是什么意思?
- bash - 备份脚本:删除旧文件不起作用
- sqlite - 通过 H2 控制台访问时无法在 SQLlite 中执行查询 - SQLite JDBC 驱动程序未实现 null/0
- angular - 带有 JqueryUI Draggable 的 Angular5.x - 触发停止事件时 UI 不会更新
- javascript - 在一个 td 上禁用点击事件 - javascript
- python - Twilio 无法将 webhook 嵌入工作室流程并让它向我发送消息
- c# - 根据鼠标点击时的位置上下移动
- android - 如何制作包含 listview + imageview 的可滚动布局
- mpi - mpirun 在 slurm 管理的集群上的任何用例?
- reactjs - 如何使用 React 在我的代码中为 componentWillReceiveProps 编写测试用例