在我的主要控制中我定义了
<Control.Resources>
<Style x:Key="TextInput" TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Text" Value="{x:Static sys:String.Empty}">
<Setter Property="Background" Value="PaleTurquoise" />
</Trigger>
</Style.Triggers>
</Style>
</Control.Resources>
如何在我的主控件中放置的另一个用户控件中使用此样式的textBox?
我不需要将样式应用于所有文本框,但我希望样式可以在我想要的任何控件中重用。没有自定义控件可以吗?
答案 0 :(得分:1)
将您的样式移至窗口资源
<Window.Resources>
<Style x:Key="TextInput" TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Text" Value="{x:Static sys:String.Empty}">
<Setter Property="Background" Value="PaleTurquoise" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
然后参考这样的风格:
<TextBox Style="{StaticResource TextInput}" />
答案 1 :(得分:1)
您应该将资源字典添加到项目中。
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="TextInput" TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Text" Value="{x:Static sys:String.Empty}">
<Setter Property="Background" Value="PaleTurquoise" />
</Trigger>
</Style.Triggers>
</Style>
然后在应用资源中添加对此词典的引用:
<Application
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="YOur class"
StartupUri="StartupWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles\TextInputStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
因此,您可以在项目的任何部分使用您的风格