XAML - 通用文本stylewith触发/参数? [英] XAML - Generic textbox stylewith triggers / parameters?

查看:106
本文介绍了XAML - 通用文本stylewith触发/参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能有一个通用的风格模板,可以切换我的文本框基于一个布尔颜色。但我不希望有创建一个独特的风格为每一个文本框。如何做到这一点?

我已经把下面我怎么会想到这个工作,一些示例code。 三个文本框,都具有不同绑定,但在尝试使用相同的模板选择基于一个布尔红色或绿色。

感谢

 < TextBlock的文本={结合文本1}风格= {DynamicResource MyTextBoxTemplate}的DataContext ={结合MyBool1}/>
< TextBlock的文本={结合文本2}风格= {DynamicResource MyTextBoxTemplate}的DataContext ={结合MyBool2}/>
< TextBlock的文本={结合文本3}风格= {DynamicResource MyTextBoxTemplate}的DataContext ={结合MyBool3}/>

            <风格X:关键=MyTextBoxTemplate的TargetType =TextBlock的>
                < Style.Triggers>
                    < D​​ataTrigger值=真绑定={结合???}>
                        < setter属性=前景VALUE =绿色/>
                    < / DataTrigger>
                    < D​​ataTrigger值=FALSE绑定={结合???}>
                        < setter属性=前景VALUE =红/>
                    < / DataTrigger>
                < /Style.Triggers>
            < /样式和GT;
 

解决方案

您可以使用的标记文本框的属性。

 < TextBlock的文本={结合文本1}风格= {DynamicResource MyTextBoxTemplate}标签={结合MyBool1}/>
< TextBlock的文本={结合文本2}风格= {DynamicResource MyTextBoxTemplate}标签={结合MyBool2}/>
< TextBlock的文本={结合文本3}风格= {DynamicResource MyTextBoxTemplate}标签={结合MyBool3}/>

<风格X:关键=MyTextBoxTemplate的TargetType =TextBlock的>
< Style.Triggers>
<触发属性=变量VALUE =真>
< setter属性=前景VALUE =绿色/>
< /触发>
<触发属性=标签值=FALSE>
< setter属性=前景VALUE =红/>
< /触发>
< /Style.Triggers>
< /样式和GT;
 

I want to be able to have a generic style template that can switch colors of my textboxes based on a bool. But I don't want to have to create a unique style for each textbox. How do I do this?

I have put some sample code below how I might expect this to work. Three textboxes, all with different bindings but attempting to use the same template to select red or green colour based on a bool.

Thanks

<TextBlock Text="{Binding Text1}" Style={DynamicResource MyTextBoxTemplate} DataContext="{Binding MyBool1}" />
<TextBlock Text="{Binding Text2}" Style={DynamicResource MyTextBoxTemplate} DataContext="{Binding MyBool2}" />
<TextBlock Text="{Binding Text3}" Style={DynamicResource MyTextBoxTemplate} DataContext="{Binding MyBool3}" />

            <Style x:Key="MyTextBoxTemplate" TargetType="TextBlock">
                <Style.Triggers>
                    <DataTrigger Value="True" Binding="{Binding ???}">
                        <Setter Property="Foreground" Value="Green" />
                    </DataTrigger>
                    <DataTrigger Value="False" Binding="{Binding ???}">
                        <Setter Property="Foreground" Value="Red" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>

解决方案

You can use the Tag property of TextBox

<TextBlock Text="{Binding Text1}" Style={DynamicResource MyTextBoxTemplate} Tag="{Binding MyBool1}" />
<TextBlock Text="{Binding Text2}" Style={DynamicResource MyTextBoxTemplate} Tag="{Binding MyBool2}" />
<TextBlock Text="{Binding Text3}" Style={DynamicResource MyTextBoxTemplate} Tag="{Binding MyBool3}" />

<Style x:Key="MyTextBoxTemplate" TargetType="TextBlock">
	<Style.Triggers>
		<Trigger Property="Tag" Value="True">
			<Setter Property="Foreground" Value="Green" />
		</Trigger>
		<Trigger Property="Tag" Value="False">
			<Setter Property="Foreground" Value="Red" />
		</Trigger>
	</Style.Triggers>
</Style>

这篇关于XAML - 通用文本stylewith触发/参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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