Как добавить эффект для текстового поля в стиль
Я пытаюсь добавить эффект к стилю, чтобы его повторно использовать, но по какой-то причине он не работает...
<Style x:Key="NumericTextBoxStyle" TargetType="{x:Type TextBox}">
<Style.Resources>
<TextBox.Effect x:Key="EffectStyle">
<DropShadowEffect BlurRadius="56"
Direction="392"
Color="#FF872E2E"
RenderingBias="Quality"/>
</TextBox.Effect>
</Style.Resources>
<Setter Property="Height" Value="25"/>
<Setter Property="Width" Value="120"/>
<Setter Property="HorizontalAlignment" Value="Right"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="TextAlignment" Value="Center"/>
</Style>
но как добавить часть стиля? (также как я объявляю для эффекта?)
спасибо
Ответы
Ответ 1
Попробуйте добавить эффект как сеттер вместо
<Style x:Key="NumericTextBoxStyle" TargetType="{x:Type TextBox}">
<Setter Property="Effect">
<Setter.Value>
<DropShadowEffect BlurRadius="56"
Direction="392"
Color="#FF872E2E"
RenderingBias="Quality"/>
</Setter.Value>
</Setter>
<Setter Property="Height" Value="25"/>
<Setter Property="Width" Value="120"/>
<Setter Property="HorizontalAlignment" Value="Right"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="TextAlignment" Value="Center"/>
</Style>
Или, если вы хотите использовать Эффект как ресурс в стиле, вы можете сделать это как
<Style x:Key="NumericTextBoxStyle" TargetType="{x:Type TextBox}">
<Style.Resources>
<DropShadowEffect x:Key="dropShadowEffect"
BlurRadius="56"
Direction="392"
Color="#FF872E2E"
RenderingBias="Quality"/>
</Style.Resources>
<Setter Property="Effect" Value="{StaticResource dropShadowEffect}"/>
<!--...-->
</Style>
Ответ 2
Вы также можете сделать свой эффект глобальным ресурсом, чтобы использовать его с другими стилями/элементами управления:
<Grid>
<Grid.Resources>
<DropShadowEffect x:Key="dropShadowEffect" BlurRadius="56"
Direction="392"
Color="#FF872E2E"
RenderingBias="Quality"/>
<Style x:Key="NumericTextBoxStyle" TargetType="{x:Type TextBox}">
<Setter Property="Effect" Value="{StaticResource dropShadowEffect}" />
<Setter Property="Height" Value="25"/>
<Setter Property="Width" Value="120"/>
</Style>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<TextBox Style="{StaticResource NumericTextBoxStyle}" />
<TextBox Style="{StaticResource NumericTextBoxStyle}" Grid.Row="1" />
<ComboBox Effect="{StaticResource dropShadowEffect}" Grid.Row="2" />
</Grid>