Arr*_*row 7 .net c# wpf xaml microsoft-metro
我有一个TextBlock:
<TextBlock x:Name="someText" Text="{Binding ElementName=theList, Path=SelectedItem.Name, Mode=TwoWay}" />
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,它被绑定到另一个元素的选定项目.现在,让我们说,例如,所选项目显示"你好".我想把它的名字附加到它上面(在XAML中,而不是代码隐藏),所以它的内容如下:"Hello,Arrow.".我怎样才能做到这一点?
kma*_*zek 11
试试这个:
<TextBlock x:Name="someText" TextWrapping="NoWrap">
<Run Text="{Binding ElementName=theList, Path=SelectedItem, Mode=TwoWay}" />
<Run Text=" Arrow." />
</TextBlock>
Run Code Online (Sandbox Code Playgroud)
Metro XAML尚未提供XAML解决方案:
您可以使用StringFormat:
<TextBlock x:Name="someText" Text="{Binding ElementName=theList, Path=SelectedItem, Mode=TwoWay, StringFormat={}{0} Arrow.}" />
Run Code Online (Sandbox Code Playgroud)
您还可以使用MultiBinding和StringFormat:
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} Arrow.">
<Binding ElementName="theList" Path="SelectedItem.Name" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
Run Code Online (Sandbox Code Playgroud)
使用此配置,您唯一可以做的就是在所选项目中包含该文本。所以,我建议的是更多这样的事情:
<StackPanel Orientation="Horizontal">
<TextBlock x:Name="someText"
Text="{Binding ElementName=theList,
Path=SelectedItem.Name,
Mode=TwoWay}" />
<TextBlock x:Name="suffixText"/>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
通过此配置,您可以提供suffixText您想要的任何方式并获得您正在寻找的结果。