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

有帮助吗?

解决方案

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>

其他提示

you can try something like

<Button>
   <Button.Content>
          <TextBlock TextAlignment="Left">  
                <Run Text="{Binding ActualWidth" RelativeSource="{RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}"/>
                <Run Text=" | " />           
                <Run Text="{Binding ActualHeight" RelativeSource="{RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}"/>
           </TextBlock>
    </Button.Content>
 </Button>

By using Run you can create whatever you want to display. and also can show on diffrent style on each Run for e.g. you can create bold effect on first run and other could be diffrent as like italic or something else.

Rether then using Height and Width properties of Window use ActualHeight and ActualWidth Properties which will provide you a actual values. there could be a chance that you will get NAN on Height and Width as they were not defined specifically.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top