用于样式的带有StaticResource的WPF TextBox和绑定到ViewModel属性的文本



我几天来一直在关注下面的问题,也许我误解了什么。

我创建了一个带有文本框样式设置的ResourceDictionary,并将其添加到App.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ZeissVorbereitung">
<Style x:Key="ModernTextbox" TargetType="{x:Type TextBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Border CornerRadius="10"
Background="#353340"
Width="100" Height="30">
<Grid>
<Rectangle StrokeThickness="1"/>
<TextBox Margin="1"
Text="{TemplateBinding Text}"
BorderThickness="0"
Background="Transparent"
VerticalContentAlignment="Center"
Padding="5"
Foreground="#CFCFCF"
x:Name="SearchBox"/>

<TextBlock IsHitTestVisible="False"
Text="Type new Entry..."
VerticalAlignment="Center"
HorizontalAlignment="Left"
Margin="10,0,0,0"
FontSize="11"
Foreground="DarkGray"
Grid.Column="1">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<DataTrigger Binding="{Binding Text, ElementName=SearchBox}" Value="">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
<Setter Property="Visibility" Value="Hidden"/>
</Style>
</TextBlock.Style>
</TextBlock>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

然后我在视图中创建一个文本框,并成功地使用样式:

<TextBox
Grid.Column="0"
Grid.Row="1"
Width="100"
Margin="0,5,0,0"
VerticalAlignment="Top"
Style="{StaticResource ModernTextbox}"
Text="{Binding AddNavigationTextField}"/>

在ViewModel中是绑定的属性:

private string _addNavigationTextField = "";
public string AddNavigationTextField
{
get => _addNavigationTextField;
set
{
_addNavigationTextField = value;
OnProtertyChanged();
}
}

绑定正常工作,但我不知道如何将样式资源与动态绑定相结合,所以我可以使用多个具有相同样式但不同绑定的TextBox。谢谢你的帮助!

试试这样的风格:

<Style x:Key="ModernTextbox" TargetType="{x:Type TextBox}">
<Setter Property="Foreground" Value="#CFCFCF"/>
<Setter Property="CaretBrush" Value="#CFCFCF"/>
<Setter Property="Padding" Value="4"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Border CornerRadius="10"
Background="#353340"
Width="100" Height="30">
<Grid>
<Rectangle StrokeThickness="1"/>
<ScrollViewer x:Name="PART_ContentHost" 
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Margin="{TemplateBinding Padding}"/>
<TextBlock IsHitTestVisible="False"
Text="Type new Entry..."
VerticalAlignment="Center"
HorizontalAlignment="Left"
Margin="10,0,0,0"
FontSize="11"
Foreground="DarkGray"
x:Name="Watermark"
Visibility="Collapsed">
</TextBlock>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="Text" Value="">
<Setter Property="Visibility"  TargetName="Watermark" Value="Visible"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

我改进了触发器,去掉了里面的texcox,并使用了standart风格的一部分。

相关内容

  • 没有找到相关文章

最新更新