从MainWindow更新属性到userControl

时间:2014-02-19 10:39:14

标签: c# wpf user-controls inotifypropertychanged

我在MainWindow“ZoomSlider”中定义了属性,它被数据绑定到Slider

UserControl Ellipse我有一个Slider,其高度限制为<Slider Grid.Column="4" Value="{Binding ElementName=MainWindow,Path=ZoomSlider,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,NotifyOnSourceUpdated=True}" Maximum="46" Minimum=".1" LargeChange=".1" Ticks="0,1,5" TickPlacement="BottomRight" SmallChange=".1" Height="22" Width="75" Margin="0" HorizontalAlignment="Right" VerticalAlignment="Bottom"> </Slider> <!--Content Control is the Host of a userControl which iam Setting dynamically at runtime--> <ContentControl Name="content" BorderThickness="1" Content="{Binding CurrentViewModel,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,NotifyOnSourceUpdated=True}"> </ContentControl>

MainWindow代码

<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> 

UserControl代码

private double zoomSlider = 2;

public double ZoomSlider
{
    get { return zoomSlider; }
    set
    {
        zoomSlider = value;
        NotifyPropertyChanged("ZoomSlider");
    }
}

  

MainWindow c#code

UserControl

我成功获取了Slider中的默认滑块值,但当UserControl的value属性发生更改时,它未反映在UserControl中。

因此,如何将更新的滑块值更改为Ellipse UserControl

注意:

{{1}}绑定到UserControl Viewmodel,MainWindow绑定到Window ViewModel

感谢任何帮助!!!!

1 个答案:

答案 0 :(得分:0)

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));