1
在當前窗口中,我有一個帶有多個控件(標籤,文本框,按鈕)的網格。 一般樣式在App.xaml中設置。網格資源中設置了每個控件的擴展。 每個控件的可見性由viewmodel屬性值決定。不要將每個控件的可見性綁定到它(它使用自定義轉換器,這會導致很多重複)我希望有「MyVisible1」風格。如何使樣式擴展但不覆蓋其他樣式
問題是如果我應用這種風格它重疊其他屬性。我應該在「BasedOn」中使用什麼值?或者我還能做些什麼來實現它?
<Grid>
<Grid.Resources>
<Style TargetType="{x:Type Control}" x:Key="MyVisible1">
<Setter Property="Visibility" Value="{Binding ...}" />
</Style>
<Style TargetType="Label" BasedOn="{StaticResource {x:Type Label}}">
<Setter Property="HorizontalAlignment" Value="Right" />
</Style>
<Style TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Width" Value="80" />
<Setter Property="HorizontalAlignment" Value="Left" />
</Style>
<Style TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Width" Value="45" />
<Setter Property="HorizontalAlignment" Value="Right" />
</Style>
</Grid.Resources>
<TextBox Grid.Column="0" Grid.Row="0" Style="{StaticResource MyVisible1}"/>
</Grid>
我將擁有多種可見性樣式。在這種情況下,你的建議並沒有幫助。 ( – Julia
當我第一次開始編寫WPF時,我總是在各種'Style',ControlTemplate','DataTemplate'等中得到大量重複的代碼。我花了很多年試圖找到複製更少代碼的方法並找到通常是不可能的,然後我意識到WPF與其他語言相比是一種相當冗長的語言,如果我們複製幾個字節甚至千字節的代碼,那真的沒有關係。 – Sheridan