我有一个 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>
在这种情况下,标签(“System.Windows.Controls.Label
”)的 .ToString()
表示形式显示为标题。
请注意,单个绑定(bind)工作正常:
<DataEditPane x:TypeArguments="MyType" x:Class="MyDataEditPane">
<DataEditPane.Header>
<Binding Path="BoundVariable1" />
</DataEditPane.Header>
</DataEditPane>
如果重要的话,我将使用 Syncfusion TabItemExt
作为继承层次结构中的父类(super class)之一,但由于该类不会覆盖 Header
属性,我不要认为这有什么不同。
我做错了什么?我知道我可以在 ViewModel 中创建另一个属性作为 header (然后对其进行单绑定(bind)),但我想了解如何在 XAML 中正确执行此操作。
最佳答案
尝试使用 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
关于c# - WPF MultiBinding 在 Syncfusion TabItemExt header 中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13256286/