我有一个宽度为auto的标签,该标签绑定到string类型的属性。
<Label x:Name="ExampleLabel" Content="{Binding ExampleProperty}"Height="30" Width="Auto" >
然后,我有一个绑定到该标签宽度的属性。转换器应将宽度转换为负值。
<UserControl.Resources>
<c:PositiveToNegativeConverter x:Key="PositiveToNegativeConverter"/>
</UserControl.Resources>
"{Binding ElementName=ExampleLabel, Path=Width, Converter={StaticResource PositiveToNegativeConverter}}"
我希望转换器在标签内容更改时执行,但在应用程序加载时仅触发一次。
这是我的转换器
public class PositiveToNegativeConverter : IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
return (double)value * -1;
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
return Math.Abs((double) value);
}
}
任何帮助深表感谢。谢谢。
如果您绑定到ActualWidth
而不是它会起作用Width
吗?Width
就是您最后一次分配给该Width
属性的任何值,而它ActualWidth
是一个实时更新的实时运行时值,用于确定事物在UI中的实际宽度。
我希望当您希望它更新时:
"{Binding ElementName=ExampleLabel, Path=ActualWidth, Converter={StaticResource PositiveToNegativeConverter}}"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句