Question

I have a subclass of TabItem as follows, for which I'm trying to set the Header property. I've tried this with a 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>

But it fails as such:

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')

I'd always thought the StringFormat served the role of the converter, but perhaps not?

Wrapping the fields together in some kind of container, like a Label, also doesn't seem to work:

<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>

In this case, the .ToString() representation of the label ("System.Windows.Controls.Label") is shown as the header.

Note that a single binding works just fine:

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

If it matters, I'm using the Syncfusion TabItemExt as one of my superclasses in the inheritance hierarchy, but as that class doesn't override the Header property I don't think that makes a difference.

What am I doing wrong? I know I can make another property in the ViewModel to act as the Header (and then single-bind that) but I want to learn how to do this properly in XAML.

Was it helpful?

Solution 2

Multi-Bindings requires a converter, i think that a converter that you may use is the StringFormatConverter, it is a IMultiValueConverter so works for multibindings. Maybe you should adapt it to your case.

Hope this could be useful for you...

OTHER TIPS

Try a TextBlock instead of a Label. The following code worked fine for me.

I tried this:

<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>

I wonder if StringFormat is only valid in cases where a string is expected rather than an object.

There's an example on MSDN here: http://msdn.microsoft.com/en-us/library/system.windows.data.bindingbase.stringformat.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top