C# 将属性从MainWindow更新为userControl
我在主窗口ZoomSlider中定义了属性,即数据绑定到Slider 在UserControl上,我有一个椭圆,它的高度以滑块值为界 主窗口代码 我成功地在UserControl中获得了默认的slider值,但是当slider的value属性更改时,它不会反映在UserControl中 那么,底线是如何将更新的滑块值获取到UserControl Ellipse 注: UserControl绑定到UserControl Viewmodel,MainWindow到Window Viewmodel也是如此C# 将属性从MainWindow更新为userControl,c#,wpf,user-controls,inotifypropertychanged,C#,Wpf,User Controls,Inotifypropertychanged,我在主窗口ZoomSlider中定义了属性,即数据绑定到Slider 在UserControl上,我有一个椭圆,它的高度以滑块值为界 主窗口代码 我成功地在UserControl中获得了默认的slider值,但是当slider的value属性更改时,它不会反映在UserControl中 那么,底线是如何将更新的滑块值获取到UserControl Ellipse 注: UserControl绑定到UserControl Viewmodel,MainWindow到Window Viewmodel也是
感谢您的帮助 ZoomSlider应具有DependencyProperty作为支持字段以启用绑定
public double ZoomSlider
{
get { return (double)GetValue(ZoomSliderProperty); }
set { SetValue(ZoomSliderProperty, value); }
}
// Using a DependencyProperty as the backing store for ZoomSlider. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ZoomSliderProperty =
DependencyProperty.Register("ZoomSlider", typeof(double), typeof(MainWindow), new PropertyMetadata(2d));
在此处放置转换器并检查。。。
<Ellipse Stroke="Black">
<Ellipse.Height>
<MultiBinding Converter="{StaticResource zoomConverter}" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" NotifyOnSourceUpdated="True">
<Binding Path="Dia" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" NotifyOnSourceUpdated="True"/>
<Binding RelativeSource="{RelativeSource FindAncestor,AncestorType={x:Type Window}}" Path="ZoomSlider" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"
NotifyOnSourceUpdated="True" PresentationTraceSources.TraceLevel="High"/>
</MultiBinding>
private double zoomSlider = 2;
public double ZoomSlider
{
get { return zoomSlider; }
set
{
zoomSlider = value;
NotifyPropertyChanged("ZoomSlider");
}
}
public double ZoomSlider
{
get { return (double)GetValue(ZoomSliderProperty); }
set { SetValue(ZoomSliderProperty, value); }
}
// Using a DependencyProperty as the backing store for ZoomSlider. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ZoomSliderProperty =
DependencyProperty.Register("ZoomSlider", typeof(double), typeof(MainWindow), new PropertyMetadata(2d));