我有一個UserControl
內StackPanel
:如何擴展從UserControl中父容器繼承的樣式?
<StackPanel>
<local:MyUC/>
</StackPanel>
的UserControl
包含具有通過ResourceDictionary
設置自己的風格,實現了統一的外觀各地應用的各種TextBoxes
和其他控制。
對於TextBox
ES,這ResourceDictionary
條目是這樣的:
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="DockPanel.Dock" Value="Top"/>
<Setter Property="TextWrapping" Value="Wrap"/>
<Setter Property="MaxWidth" Value="{Binding RelativeSource={RelativeSource AncestorType=Border},Path=ActualWidth}"/>
<Setter Property="VerticalScrollBarVisibility" Value="Auto"/>
</Style>
現在我想在父控件(StackPanel
)所有UserControl
內TextBox
ES的應用默認的風格,所以我可以TextBox
ES的IsReadOnly
- 屬性綁定到只有在父母模型得到的屬性:
<StackPanel>
<StackPanel.Resources>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Style.Triggers>
<DataTrigger Binding="{Binding Model.State}" Value="ReadOnly">
<Setter Property="IsReadOnly" Value="True"/>
</DataTrigger>
</Style.Triggers>
</Style>
</StackPanel.Resources>
<local:MyUC/>
</StackPanel>
不幸的是,爲TargetType="{x:Type TextBox}"
的默認樣式在UserControl
的ResourceDictionary
內定義的內容覆蓋了我嘗試在父項StackPanel
中設置的樣式。
我怎樣才能實現對延長這是StackPanel.Resources
內與在UserControl
小號ResourceDictionary
,而不是覆蓋它定義的默認樣式定義默認樣式?
感謝您的文章,我最終爲UserControl的TextBoxes添加了一個額外的ContainerControl,並將它的IsReadOnly屬性綁定到我的ViewModelBase中可用的通用ReadOnly屬性。當我在半年前通過問題時,很遺憾,我無法立即測試您的解決方案,但會一直記住,只要我再次遇到問題,就試一試。 – M463