更改“标签”内的属性属性编程 [英] Change property inside "Tag" property programatically

查看:60
本文介绍了更改“标签”内的属性属性编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!我的App.xaml中有以下样式:

Hello everyone! I have the following style in my App.xaml:

<!--AboutBackButton-->
<Style x:Key="AboutBackButton" TargetType="Button">
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="BorderBrush" Value="{x:Null}"/>
    <Setter Property="BorderThickness" Value="0"/>
    <Setter Property="Template">
        <Setter.Value>						                                    
            <ControlTemplate TargetType="Button">							                
                <Image Source="{TemplateBinding Tag}"									 
                              VerticalAlignment="Center"									 
                              HorizontalAlignment="Center"									 
                              Height="16"									 
                              Width="16"/>						 
            </ControlTemplate>					 
        </Setter.Value>
    </Setter>
</Style>




然后,我在下面使用它方式:

Then, I use it in the following way:

<Button x:Name="ButtonAbout"
		Grid.Column="2"
		Style="{StaticResource AboutBackButton}"
		Click="ButtonAbout_Click">
	<Button.Tag>
		<ImageSource>/Resources/information.png</ImageSource>
	</Button.Tag>
</Button>




但现在我需要更改内容of< ImageSource>编程。特别是,当布尔变量的值改变时。我怎么能这样做?

But now I need to change the content of <ImageSource> programatically. Specifically, when the value of a boolean variable changes. How I can do this?

推荐答案

你好阿尔弗雷多,

Hi Alfredo,

我修改你的风格,您可以在代码后面更改Button标签,如下所示:

I modify your style, and you can change Button tag in code behind like this:

 <Style x:Key="AboutBackButton" TargetType="Button">
            <Setter Property="Background" Value="Transparent" />
            <Setter Property="BorderBrush" Value="{x:Null}" />
            <Setter Property="BorderThickness" Value="0" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Image
                            Width="30"
                            Height="30"
                            HorizontalAlignment="Center"
                            VerticalAlignment="Center"
                            Source="{Binding Path=Tag, RelativeSource={RelativeSource TemplatedParent}}" />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>




 private void ButtonAbout_Click(object sender, RoutedEventArgs e)
        {
           
            ButtonAbout.Tag = "..\\Photo\\2.jpg";
        }

最好的问候,

Cherry


这篇关于更改“标签”内的属性属性编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆