我正在尝试实现一个UserControl
,它承载与其某些属性绑定的其他控件。在我的情况下,它会在NumericUpDown
旁边显示一个滑块 - 两个控件都将绑定到UserControl
的{{1}}属性。我的方法是为每个控件定义Value
,因此在使用此控件时,仍可以通过将新滑块设置为相应属性的值来更改DependencyProperty
,Minimum
等属性:
TickPlacement
代码隐藏看起来像这样:
<!-- Displays a slider with default properties next to a text block-->
<Foo/>
<!-- Displays a sider with custom properties -->
<Foo>
<Foo.Slider>
<Slider Maximum="500" TickPlacement="Both" TickFrequency="20"/>
</Foo.Slider>
</Foo>
XAML代码的相关部分如下所示:
public partial class Foo : UserControl
{
public Foo()
{
InitializeComponent();
Slider = new Slider();
}
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(nameof(Value), typeof(double), typeof(Foo), new PropertyMetadata(0.0));
public double Value
{
get => (double)GetValue(ValueProperty);
set => SetValue(ValueProperty, value);
}
public static readonly DependencyProperty SliderProperty = DependencyProperty.Register(nameof(Slider), typeof(Slider), typeof(Foo),
new PropertyMetadata(
new PropertyChangedCallback((obj, e) =>
{
Slider slNew = (Slider)e.NewValue;
if (slNew == null)
obj.SetValue(SliderProperty, new Slider());
else
slNew.SetBinding(ValueProperty,
new Binding("Value")
{
Source = obj,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
Mode = BindingMode.TwoWay
});
})));
public Slider Slider
{
get => (Slider)GetValue(SliderProperty);
set => SetValue(SliderProperty, value ?? new Slider());
}
}
实际问题是从代码隐藏创建的绑定似乎不起作用。我假设这是由于以下原因:
<UserControl x:Name="ctrlBase" [...]>
<Grid [...]>
<Grid.ColumnDefinitions [...]/>
<ContentControl DataContext="{Binding ElementName=ctrlBase}"
Content="{Binding Path=Slider}"/>
<!-- NumericUpDown to be implemented -->
<TextBlock Grid.Column="1"
Text="{Binding ElementName=ctrlBase, Path=Value}/>
</Grid>
</UserControl>
属性更改
文本块显示的值,但不是滑块的值。答案 0 :(得分:1)
您使用了错误的ValueProperty
。它应该是Slider类之一,而不是Foo.ValueProperty
:
slNew.SetBinding(Slider.ValueProperty, new Binding("Value") { ... });
如果您使用Slider.ValueProperty.AddOwner
代替DependencyProperty.Register
,那将会奏效。
也就是说,一个更简单的方法可能是简单地在UserControl的XAML中声明一个Slider,并在实例化过程中在Foo资源中提供一个默认的Slider Style:
<Foo ...>
<Foo.Resources>
<Style TargetType="Slider">
...
</Style>
</Foo.Resources>
</Foo>