我有在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
。
因此,底線如何更新滑塊值到達UserControl
Ellipse
注:
UserControl
綁定到用戶控件視圖模型,所以是主窗口到窗口視圖模型
任何幫助感謝!
在此放置的轉換器'<綁定的RelativeSource = 「{的RelativeSource FindAncestor,AncestorType = {X:類型窗口}}」 路徑=「ZoomSlider 「Mode =」TwoWay「UpdateSourceTrigger =」PropertyChanged「 NotifyOnSourceUpdated =」True「PresentationTraceSources.TraceLevel =」High「/>'並檢查... – Sankarann