如何在切换按钮IsChecked属性上更改ContentControl的内容

时间:2014-01-21 13:00:42

标签: c# wpf triggers contentcontrol

目前我正致力于在切换按钮IsChecked Proprty上更改容器的内容(作为图像)。 所以我认为ContentControl对容器来说是个不错的选择。但我无法弄清楚如何实现这一结果。

我在windows.resource

中创建了一个图像资源

<Image Source="Resources/Desert.jpg" x:Key="image1"/>
    <Image Source="Resources/Koala.jpg" x:Key="image2"/>
    <Image Source="Resources/Lighthouse.jpg" x:Key="image3"/>
    <Image Source="Resources/Chrysanthemum.jpg" x:Key="image4"/>

所以我想使用上面的资源来改变ContentControl的Content属性,方法是使用Triggers更改ControlTemplate属性,其中SourceName为(ToggleButton),TargetName为(ContentControl),但它不起作用

那么如何在toggleButton Ischeck属性上更改ContentControl的内容。

编辑

<Image Source="Resources/Desert.jpg" x:Key="image1"/>
    <Image Source="Resources/Koala.jpg" x:Key="image2"/>
    <Image Source="Resources/Lighthouse.jpg" x:Key="image3"/>
    <Image Source="Resources/Chrysanthemum.jpg" x:Key="image4"/>

我刚试过,因为我不知道该怎么办.....

欢迎任何帮助

1 个答案:

答案 0 :(得分:1)

你似乎不必要地过度复杂的情况。您只能使用Image控件而不是ContentControl。要在两个Image之间切换,您可以使用DataTrigger

执行此操作
<Image Source="Resources/Desert.jpg" x:Key="image1"/>
<StackPanel>
    <ToggleButton Name="Button" Content="Change Image" Margin="10" />
    <Image Margin="10,0,10,10">
        <Image.Style>
            <Style>
                <Setter Property="Image.Source" 
Value="/YourAppName;component/Resources/Desert.jpg" />
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsChecked, ElementName=Button}" 
Value="True">
                        <Setter Property="Image.Source" 
Value="/YourAppName;component/Images/Resources/Koala.jpg" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Image.Style>
    </Image>
</StackPanel>