【问题标题】:WPF MultiBinding fails in Syncfusion TabItemExt headerWPF MultiBinding 在 Syncfusion TabItemExt 标头中失败
【发布时间】:2012-11-06 17:32:30
【问题描述】:

我有一个TabItem 的子类,如下所示,我正在尝试为其设置Header 属性。我用MultiBinding 试过这个:

<DataEditPane x:TypeArguments="MyType" x:Class="MyDataEditPane">
    <DataEditPane.Header>
        <MultiBinding StringFormat="Hello world {0} {1}">
            <Binding Path="BoundVariable1" />
            <Binding Path="BoundVariable2" />
        </MultiBinding>    
    </DataEditPane.Header>
</DataEditPane>

但它失败了:

System.Windows.Data Error: 28 : MultiBinding failed because it has no valid Converter. MultiBindingExpression:target element is 'MyDataEditPane' (Name=''); target property is 'Header' (type 'Object')
System.Windows.Data Error: 28 : MultiBinding failed because it has no valid Converter. MultiBindingExpression:target element is 'MyDataEditPane' (Name=''); target property is 'Header' (type 'Object')

我一直认为StringFormat 充当转换器的角色,但也许不是?

将字段包装在某种容器中,例如Label,似乎也不起作用:

<DataEditPane x:TypeArguments="MyType" x:Class="MyDataEditPane">
    <DataEditPane.Header>
        <Label>
            <Label.Text>
                <MultiBinding StringFormat="Hello world {0} {1}">
                    <Binding Path="BoundVariable1" />
                    <Binding Path="BoundVariable2" />
                </MultiBinding>    
            </Label.Text>
        </Label>
    </DataEditPane.Header>
</DataEditPane>

在这种情况下,标签的.ToString() 表示(“System.Windows.Controls.Label”)显示为标题。

请注意,单个绑定可以正常工作:

<DataEditPane x:TypeArguments="MyType" x:Class="MyDataEditPane">
    <DataEditPane.Header>
        <Binding Path="BoundVariable1" />
    </DataEditPane.Header>
</DataEditPane>

如果重要的话,我在继承层次结构中使用 Syncfusion TabItemExt 作为我的超类之一,但由于该类不会覆盖 Header 属性,我认为这没有什么不同。

我做错了什么?我知道我可以在 ViewModel 中创建另一个属性作为 Header(然后单绑定),但我想学习如何在 XAML 中正确执行此操作。

【问题讨论】:

    标签: c# wpf xaml multibinding syncfusion


    【解决方案1】:

    尝试使用 TextBlock 而不是 Label。以下代码对我来说很好。

    我试过这个:

    <Window x:Class="ListBox.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:ListBox" Title="Window1" Height="300" Width="300">
        <Window.DataContext>
            <local:TextVM/>
        </Window.DataContext>
        <StackPanel>
            <TextBox Text="{Binding Text1}"  />
            <TextBox Text="{Binding Text2}" />
            <TextBlock>
                <TextBlock.Text>
                    <MultiBinding StringFormat="Hello World {0} - {1}">
                        <Binding Path="Text1" />
                        <Binding Path="Text2" />
                    </MultiBinding>
                </TextBlock.Text>
            </TextBlock>
        </StackPanel>
    </Window>
    

    我想知道 StringFormat 是否仅在需要字符串而不是对象的情况下才有效。

    这里有一个 MSDN 上的示例:http://msdn.microsoft.com/en-us/library/system.windows.data.bindingbase.stringformat.aspx

    【讨论】:

    • 谢谢,但没有骰子。将您的 XAML 放在 &lt;DataEditPane.Header&gt; 标记中只会在标题中显示 System.Windows.Controls.StackPanel。 Header 属性的某些内容只允许文本。
    【解决方案2】:

    多绑定需要一个转换器,我认为您可能使用的转换器是StringFormatConverter,它是IMultiValueConverter,因此适用于多绑定。 也许你应该根据你的情况调整它。

    希望这对你有用...

    【讨论】:

    • 是的,看来我希望在没有转换器的情况下仅在 XAML 中执行此操作已破灭。感谢您的回复!
    • 明确一点——多重绑定不需要转换器。如果绑定到字符串类型的属性,StringFormat 可以正常工作。在这种情况下,Syncfusion TabItemExt 控件显然会在标题内的任何内容上调用 ToString,但这与绑定无关。
    • 是的,如果是字符串类型属性,则不需要在 XML 代码中显式编写。但实际上所有的Bindings都有一个转换器,只是大多数情况下WPF都使用一个默认的Converter。在这种情况下,当是字符串类型属性时,它使用默认转换器(我认为是 StringFormatConverter)
    猜你喜欢
    • 2011-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-22
    • 2011-03-09
    • 2019-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多