【问题标题】:Issue to bind more than one property into single element property将多个属性绑定到单个元素属性的问题
【发布时间】:2013-06-10 13:39:42
【问题描述】:
    <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>

这里我尝试将窗口的宽度和高度绑定到按钮内容中,但没有任何意义。

【问题讨论】:

  • StringFormat 要求目标是字符串类型。
  • 你想达到什么目标?我不明白。如果您使用多重绑定,您还应该使用多重转换器。

标签: wpf multibinding


【解决方案1】:

正如 Adrian 所建议的,您必须将 StringFormat 绑定的结果分配给文本控件。试试这个:

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

【讨论】:

    【解决方案2】:

    你可以试试

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

    通过使用 Run,您可以创建任何您想要显示的内容。并且还可以在每个 Run 上显示不同的样式,例如您可以在第一次运行时创建大胆的效果,其他可能会有所不同,例如斜体或其他。

    然后使用 Window 的 HeightWidth 属性使用 ActualHeightActualWidth 属性,这将为您提供实际值。您可能会在高度和宽度上获得 NAN,因为它们没有具体定义。

    【讨论】:

      猜你喜欢
      • 2011-09-01
      • 2013-11-05
      • 1970-01-01
      • 2022-01-01
      • 2023-04-04
      • 1970-01-01
      • 2011-04-28
      • 2018-06-04
      相关资源
      最近更新 更多