我可以在DataTrigger中更改绑定的属性,而不知道绑定本身吗? [英] Can I change the properties of a binding in a DataTrigger, without knowing the binding itself?

查看:201
本文介绍了我可以在DataTrigger中更改绑定的属性,而不知道绑定本身吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 TextBox 样式,如果框未聚焦,则格式化数字,但是在编辑时留下数字未格式化。



这是我想要多个TextBox的样式,但它们都包含不同的文本绑定。常规文本设置器和触发文本设置器之间的唯一区别是触发器在绑定中具有 StringFormat = N2



/ p>有没有办法让这个样式泛型,比如只改变DataTrigger中绑定的 StringFormat 属性?

 < TextBox> 
< TextBox.Style>
< Style TargetType ={x:Type TextBox}>
< Setter Property =TextValue ={Binding SomeValue,StringFormat = N2,UpdateSourceTrigger = PropertyChanged,ValidatesOnDataErrors = True}/>
< Style.Triggers>
< Trigger Property =IsKeyboardFocusWithinValue =True>
< Setter Property =TextValue ={Binding SomeValue,UpdateSourceTrigger = PropertyChanged,ValidatesOnDataErrors = True}/>
< / Trigger>
< /Style.Triggers>
< / Style>
< /TextBox.Style>
< / TextBox>


解决方案

我看到的唯一选择是创建一个StringFormat的附加属性,并使用一个multiBinding。



不是你想要的,但足够接近,我猜...



你有更多关于这个(重复)的问题的信息:



通过使用样式修改TextBox文本绑定的参数


I have a TextBox style that formats a number if the box is unfocused, but leaves the number unformatted whlie it's being edited.

This is the style I want for multiple number TextBoxes, but they all contain different Text bindings. The only difference between the regular Text setter and the Triggered Text setter is that the Triggered one has StringFormat=N2 in the binding.

Is there a way I can make this style generic, such as only changing the StringFormat property of the binding in the DataTrigger?

<TextBox>
    <TextBox.Style>
        <Style TargetType="{x:Type TextBox}">
                <Setter Property="Text" Value="{Binding SomeValue, StringFormat=N2, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" />
            <Style.Triggers>
                <Trigger Property="IsKeyboardFocusWithin" Value="True">
                    <Setter Property="Text" Value="{Binding SomeValue, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>

解决方案

The only option I see there would be to create an attached property for the StringFormat and use a multiBinding.

Not quite what you wanted, but close enough, I guess...

you have more info on this (kind of duplicate) question on S.O.:

Modifying the Parameters of a TextBox's Text Binding through the use of a Style

这篇关于我可以在DataTrigger中更改绑定的属性,而不知道绑定本身吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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