WPF自动标记标签

时间:2014-08-11 15:38:18

标签: wpf xaml

<Label Content="{Binding UpdateSourceTrigger=PropertyChanged, Mode=OneWay, Path=ViewTitle}" Height="28" HorizontalAlignment="Right" Margin="0,69,48,0" Name="labelScreenTitle" VerticalAlignment="Top" Width="{Binding}" FontWeight="Bold" FontFamily="Arial" FontSize="15" />

这是标签的xaml。 ViewTitle具有我需要显示的文本值。由于屏幕之间的长度不同,我需要将其设置为自动。我收到以下错误:

System.Windows.Data Error: 1 : Cannot create default converter to perform 'one-way' conversions between types 'MicroLabQuant.ViewModels.MainViewModel' and 'System.Double'. Consider using Converter property of Binding. BindingExpression:Path=; DataItem='MainViewModel' (HashCode=9080996); target element is 'Label' (Name='labelScreenTitle'); target property is 'Width' (type 'Double')

System.Windows.Data错误:5:BindingExpression生成的值对目标属性无效。值=&#39; MicroLabQuant.ViewModels.MainViewModel&#39; BindingExpression:路径=;的DataItem =&#39; MainViewModel&#39; (的HashCode = 9080996);目标元素是&#39;标签&#39; (名称=&#39; labelScreenTitle&#39);目标属性是&#39;宽度&#39; (键入&#39; Double&#39;)

我知道它需要转换,因为我们正在尝试设置宽度,这是一个双倍。但是你如何使用转换器呢?谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

只需删除宽度绑定...默认情况下,您的标签会根据内容自动调整大小。

<Label Content="{Binding UpdateSourceTrigger=PropertyChanged, Mode=OneWay, Path=ViewTitle}" Height="28" HorizontalAlignment="Right" Margin="0,69,48,0" Name="labelScreenTitle" VerticalAlignment="Top" FontWeight="Bold" FontFamily="Arial" FontSize="15" />