转换器仅触发一次,而不是每次标签内容更改时触发
Converter only firing once and not every time label content changes
我有宽度为 auto 的标签,它绑定到 属性 类型的字符串。
<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
instead of Width
是否有效? Width
只是 you 最后分配给 Width
属性 的任何值,而 ActualWidth
是只读的实时更新运行时UI 中事物的实际宽度值。
我希望它会在您需要时更新:
"{Binding ElementName=ExampleLabel, Path=ActualWidth, Converter={StaticResource PositiveToNegativeConverter}}"
我有宽度为 auto 的标签,它绑定到 属性 类型的字符串。
<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
instead of Width
是否有效? Width
只是 you 最后分配给 Width
属性 的任何值,而 ActualWidth
是只读的实时更新运行时UI 中事物的实际宽度值。
我希望它会在您需要时更新:
"{Binding ElementName=ExampleLabel, Path=ActualWidth, Converter={StaticResource PositiveToNegativeConverter}}"