Search code examples
wpfmultibinding

Issue to bind more than one property into single element property


    <Button>
        <Button.Content>
            <MultiBinding StringFormat="{}{0},{1}">
                <Binding Path="Width" RelativeSource="{RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}"/>
                <Binding Path="Height" RelativeSource="{RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}"/>                            
            </MultiBinding>
        </Button.Content>
    </Button>

Here i tried to bind the window's width and height into button content but it doesn't make sense.


Solution

  • As Adrian suggested, you have to assign the result of a StringFormat binding to a text control. Try this instead:

      <Button>
        <Button.Content>
            <TextBlock>
                <TextBlock.Text>
                    <MultiBinding StringFormat="{}{0},{1}">
                        <Binding Path="ActualWidth" RelativeSource="{RelativeSource Mode=FindAncestor,AncestorType={x:Type Button}}"/>
                        <Binding Path="ActualHeight" RelativeSource="{RelativeSource Mode=FindAncestor,AncestorType={x:Type Button}}"/>                            
                    </MultiBinding>
                </TextBlock.Text>
            </TextBlock>
        </Button.Content>
    </Button>