我想在后面的代码上更改图像源,比如主题

时间:2017-06-13 14:05:18

标签: c# wpf

我的xaml上有图像。像这些......

p = "rr"
print x

我想写这样的ImageSources并在我的代码端轻松更改所有Source路径。像这样:

<Image Name="ImageTekSayfa" Source="Resources/Dark/teksayfa.png" Margin="2,0" MouseLeftButtonUp="TekSayfa_Clicked"/>
<Image Name="ImageFavorites" Source="Resources/Dark/favorites.png" Margin="2,0" MouseLeftButtonUp="FavoritesClick"/>
<Image Name="ImageSoruModu" Source="Resources/Dark/sorumodu.png" Margin="2,0" MouseLeftButtonUp="SoruModu_Clicked"/>
<Image Name="ImagePerde"  Source="Resources/Dark/tahta.png" Margin="2,0" MouseLeftButtonUp="PerdeClicked"/>
<Image Name="ImagePen" Source="Resources/Dark/kalem.png" Margin="2,0" MouseLeftButtonUp="DrawingClicked"/>
<Image Name="ImageEraser" Source="Resources/Dark/silgi.png" Margin="2,0" MouseLeftButtonUp="EraserClicked"/>

等。

1 个答案:

答案 0 :(得分:1)

两种可能的方法:

  1. ValueConverter - 将每个“主题模板化”源存储为资源中的字符串 字典并使用IValueConverter的自定义实现绑定Image Source,正确填充所需的Uri:

    <Image Name="ImageTekSayfa" Source="{Binding Source={StaticResource teksayfa}, Converter={StaticResource themeConverter}}" Margin="2,0" MouseLeftButtonUp="TekSayfa_Clicked"/>
    
  2. 自定义XAML行为 - 创建可将“主题模板化”源转换为所需Uri的自定义行为:

    <Image Name="ImageTekSayfa" Margin="2,0" MouseLeftButtonUp="TekSayfa_Clicked">
        <i:Interaction.Behaviors>
            <bhv:ThemeImageSourceBehavior Source="Resources/{THEME_NAME}/teksayfa.png" />
        </i:Interaction.Behaviors>
    </Image>