2014-02-19 94 views
0

我有在MainWindow「ZoomSlider」中定義的屬性,即數據綁定到Slider從MainWindow更新屬性到userControl

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> 

用戶控件代碼

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

主窗口C#代碼

private double zoomSlider = 2; 

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

我成功地獲取默認滑塊值在UserControl,但是當Slider的價值屬性更改其是沒有得到體現在UserControl

因此,底線如何更新滑塊值到達UserControlEllipse

注:

UserControl綁定到用戶控件視圖模型,所以是主窗口到窗口視圖模型

任何幫助感謝!

+0

在此放置的轉換器'<綁定的RelativeSource = 「{的RelativeSource FindAncestor,AncestorType = {X:類型窗口}}」 路徑=「ZoomSlider 「Mode =」TwoWay「UpdateSourceTrigger =」PropertyChanged「 NotifyOnSourceUpdated =」True「PresentationTraceSources.TraceLevel =」High「/>'並檢查... – Sankarann

回答

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

感謝您的好友,您使我的一天.... –

相關問題