gpt4 book ai didi

wpf - 从 DependencyObject 继承的自定义 IValueConverter

转载 作者:行者123 更新时间:2023-12-04 05:20:09 24 4
gpt4 key购买 nike

我想尝试能够有一个转换器,其参数可以与当前数据上下文绑定(bind)。谁能告诉我为什么到达 Convert() 函数时,Source 属性始终为空?

namespace WpfApplication32
{
public class ConverterTest : DependencyObject, IValueConverter
{
public static readonly DependencyProperty SourceProperty =
DependencyProperty.Register("Source", typeof(DependencyObject), typeof(ConverterTest));

public DependencyObject Source
{
get { return (DependencyObject)this.GetValue(SourceProperty); }
set { this.SetValue(SourceProperty, value); }
}

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
}

public partial class MainWindow : Window
{
public MainWindow()
{
Value = 7;

InitializeComponent();
DataContext = this;
}

public float Value
{
get;
set;
}
}
}




<Window x:Class="WpfApplication32.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication32">

<Slider>
<Slider.Value>
<Binding Path="Value">
<Binding.Converter>
<local:ConverterTest Source="{Binding}"/>
</Binding.Converter>
</Binding>
</Slider.Value>
</Slider>

</Window>

最佳答案

一种可能的解决方案是让您的转换器从 继承。可卡住 而是(Hillberg Freezable trick)。然后,您甚至可以在 Resources 中定义 Converter 并在绑定(bind)中将其作为属性而不是额外的子元素引用。

关于wpf - 从 DependencyObject 继承的自定义 IValueConverter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13778231/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com