簡體   English   中英

如何在 wpf 中綁定具有應用樣式的文本框

[英]how to bind a textbox that has an applied style in wpf

我使用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}" />

(下圖中的MessageBox是單擊按鈕后顯示的)

應用Style時的樣子:

在此處輸入圖像描述


當我刪除Style時它的樣子:

在此處輸入圖像描述


即使我將Text="{TemplateBinding Text}"添加到我的ResourceDictionary ,綁定仍然不起作用(如果Style應用於我的TextBox


我現在通過以下方式修復它:添加在ResourceDictionary中的TextBox控件的Text屬性:

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

您可以將樣式用作單獨的文件,如下所示:
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>

主題/文本框.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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM