[英]how to bind a textbox that has an applied style in wpf
I style my TextBox
using a ResourceDictionary
(and have registered it in App.xaml
):我使用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>
My App.xaml
:我的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>
When I apply the style to my TextBox
, my binding doesn't work.当我将样式应用于我的TextBox
时,我的绑定不起作用。
My 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}" />
(the MessageBox
es on the images below are shown after the button click) (下图中的MessageBox
是单击按钮后显示的)
What it looks like when I APPLY the Style
:我应用Style
时的样子:
What it looks like when I REMOVE the Style
:当我删除Style
时它的样子:
Even if I add Text="{TemplateBinding Text}"
to my ResourceDictionary
, the binding will still not work (if the Style
is applied to my TextBox
)即使我将Text="{TemplateBinding Text}"
添加到我的ResourceDictionary
,绑定仍然不起作用(如果Style
应用于我的TextBox
)
I now fixed it by: Adding in my TextBox
control's Text
property that is in the ResourceDictionary
:我现在通过以下方式修复它:添加在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" />
You can use style as separate file like this:您可以将样式用作单独的文件,如下所示:
App.xaml: 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>
Theme/TextBoxes.xaml:主题/文本框.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>
Or use style inside或者在里面使用样式
<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.