Wpf 基于使用数据模板的其他样式创建样式
我有一个使用文本框的列表框样式,定义如下:Wpf 基于使用数据模板的其他样式创建样式,wpf,Wpf,我有一个使用文本框的列表框样式,定义如下: <Style x:Key="MyListBoxStyle" TargetType="ListBox"> <Setter Property="Background" Value="LightGray"/> <Setter Property="BorderThickness" Value="2"/> <Setter Property="BorderBru
<Style x:Key="MyListBoxStyle" TargetType="ListBox">
<Setter Property="Background" Value="LightGray"/>
<Setter Property="BorderThickness" Value="2"/>
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<TextBox Text="{Binding Mode=OneWay}" Background="LightGray"/>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
我将此样式用于许多列表框显示只读数据
我还有一组列表框,在其中我希望能够单击其中的一个文本框,然后执行一些操作。所以我想做的是在文本框上附加一个点击事件。理想情况下,我希望利用上述样式,而不必全部重新键入,只需添加添加事件绑定的新样式。你会怎么做呢?
Style.BaseOn
<Style x:Key="MyListBoxStyle" TargetType="ListBox">
<Setter Property="Background" Value="LightGray"/>
<Setter Property="BorderThickness" Value="2"/>
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<TextBox Text="{Binding Mode=OneWay}" Background="LightGray"/>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="MyListBoxStyle2" BasedOn="{StaticResource MyListBoxStyle}" TargetType="ListBox">
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<TextBox Text="{Binding Mode=OneWay}" Background="LightGray" GotFocus="TextBox_GotFocus"/>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
TryStyle。基于
property,您可以基于现有的样式添加或覆盖某些属性。因此,正如我看到的,我可以基于样式,但仍然需要复制数据模板并在那里添加所需的项…@kw1jybo,因为您需要覆盖要更改的属性