【问题标题】:how to bind a textbox that has an applied style in wpf如何在wpf中绑定具有应用样式的文本框
【发布时间】:2021-05-29 07:14:22
【问题描述】:

我使用ResourceDictionary 为我的TextBox 设置样式(并已在App.xaml 中注册):

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="{x:Type TextBox}"
           x:Key="TextBoxTheme">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBox}">
                    <Border CornerRadius="12"
                            Background="#2E3135"
                            Width="{TemplateBinding Width}"
                            Height="{TemplateBinding Height}">
                        <Grid>
                            <TextBox BorderThickness="0"
                                     Background="Transparent"
                                     VerticalContentAlignment="Center"
                                     Padding="20,0,20,0"
                                     Foreground="#B8BDC1" />
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

我的App.xaml

<Application x:Class="Paraject.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:Paraject"
             StartupUri="/MVVM/Views/Windows/LoginWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/UiDesign/Theme/TextBoxTheme.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

当我将样式应用到我的 TextBox 时,我的绑定不起作用。

我的TextBox

<TextBox x:Name="UsernameTextbox"
            Grid.Row="2"
            Width="303"
            Height="38"
            Margin="0,0,0,30"
            FontSize="15"
            Text="{Binding Path=CurrentUserAccount.Username}"
            Style="{StaticResource TextBoxTheme}" />

(下图中的MessageBoxes在按钮点击后显示)

当我应用Style时的样子:


当我删除Style时的样子:


即使我将Text="{TemplateBinding Text}" 添加到我的ResourceDictionary,绑定仍然不起作用(如果Style 应用于我的TextBox


我现在通过以下方式修复它: 添加我的TextBox 控件的Text 属性,该属性位于ResourceDictionary

<TextBox Text="{Binding Path=Text,
            RelativeSource={RelativeSource TemplatedParent}, 
            Mode=TwoWay,
            UpdateSourceTrigger=PropertyChanged}"
            BorderThickness="0"
            Background="Transparent"
            VerticalContentAlignment="Center"
            Padding="20,0,20,0"
            Foreground="#B8BDC1"
            CaretBrush="#B8BDC1" />

【问题讨论】:

    标签: c# wpf binding textbox resourcedictionary


    【解决方案1】:

    您可以像这样将样式用作单独的文件:
    App.xaml:

    <Application x:Class="SomeClassHere"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <Application.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="Theme/TextBoxes.xaml" />
                    <ResourceDictionary Source="Theme/AnotherFile.xaml" />
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Application.Resources>
    </Application>
    

    主题/TextBoxes.xaml:

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
       <Style TargetType="{x:Type TextBox}"
              x:Key="TextBoxTheme">
          ...
       </Style>
    </ResourceDictionary>
    

    或者在里面使用样式

    <Application x:Class="SomeClassHere"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <Application.Resources>
            <ResourceDictionary>
                <Style TargetType="{x:Type TextBox}"
                       x:Key="TextBoxTheme">
                    ...
                 </Style>
            </ResourceDictionary>
        </Application.Resources>
    </Application>
    

    【讨论】:

    • 我已经这样做了...我会明确地将其添加到我的帖子中...
    • @Hacki 您尝试使用Source="/UiDesign/Theme/TextBoxTheme.xaml" 而不使用第一个斜杠? Source="UiDesign/...
    • 是的,我做到了。通过做一些研究,我认为这里的问题是“关注”我的 UsernameTextBox 控件,当我不应用样式时,我可以专注于我的文本框,而当应用样式时,焦点就消失了......
    猜你喜欢
    • 2014-10-07
    • 2011-06-03
    • 2016-07-22
    • 1970-01-01
    • 2011-03-29
    • 2010-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多