【问题标题】:Wpf Multibinding in a template or style模板或样式中的 Wpf 多绑定
【发布时间】:2019-03-20 00:59:53
【问题描述】:

我有一个具有许多属性(不在列表中)的类,这些属性必须在视图中可切换。转换器本身使用多重绑定可以正常工作。

            <TextBox Grid.Row="1" Grid.Column="5">
                <TextBox.Text>
                    <MultiBinding Converter="{StaticResource IntValueConvertor}">
                        <Binding Path="property1" />
                        <Binding Path="IntegerDisplay" />
                    </MultiBinding>
                </TextBox.Text>
            </TextBox>

在代码中,“IntegerDisplay”是在我的 VM 中定义的属性。 Property1 是必须以不同方式查看的众多属性之一(取决于 IntegerDisplay)。

我想要避免的是需要将整个多绑定转换器添加到每个文本框。

这种风格的东西:

            <TextBox
                Grid.Row="1"
                Grid.Column="4"                    
                Text="{Binding Path=Property1, Converter={StaticResource IntValueConvertor}}" />

我知道这段代码不起作用!

我尝试使用样式,但无法获得 property1 的值。 它是使用样式的最佳方式还是数据模板更好?

亲切的问候

【问题讨论】:

  • 使用数据模板...
  • 嗨,你能用一些代码解释一下吗?

标签: wpf xaml multibinding wpf-style


【解决方案1】:

我建议坚持一种风格,这是一个基本的开始。

<!--This will go in your resources-->
<Style x:Name="TextBoxStyle" TargetType="{x:Type TextBox}">
            <Setter Property="Text" Value="{Binding Path=Property1, Converter={StaticResource IntValueConverter}, ConverterParameter=IntegerDisplay}"/>
</Style>

<!--This will go in your Display-->
        <TextBox Style="{StaticResource TextBoxStyle}"/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-09
    • 2011-02-02
    • 1970-01-01
    • 2013-07-10
    • 1970-01-01
    相关资源
    最近更新 更多