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

发布于 2025-01-09 22:08:59 字数 3274 浏览 0 评论 0原文

几天来我一直在关注以下问题,也许我误解了一些东西。

我创建一个带有文本框样式设置的 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();
        }
    }

通常绑定有效,但我不知道如何将样式资源与动态绑定结合起来,这样我就可以使用具有相同样式但不同绑定的多个文本框。感谢您的帮助!

I stucked at followed issue since a few days, perhaps I have misunderstood something.

I create a ResourceDictionary with stylesettings for textboxes and add it to 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>

Then I create a textbox in a View and use the Style sucessfully:

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

In the ViewModel is the property for binding:

 private string _addNavigationTextField = "";

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

Normaly the bindings works, but I dont know how to combine the Style-Resource with a dynamic Binding, so I could use multiple TextBoxes with the same Style but different bindings. Thank you for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

单身情人 2025-01-16 22:08:59

尝试这样的风格:

<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>

我改进了触发器,删除了文本框内部并使用了部分标准风格。

Try style like this:

<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>

I improve trigger, remove inside texbox and use part of standart style.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文