WPF 将 TextBox 与 ResourceDictionary 绑定

WPF binding TextBox with ResourceDictionary

我有一个 TextBox,出于设计目的,我为它制作了一个 ResourceDictionary。如何将它的 Text 属性 绑定到 MVVM 架构中的 ViewModel?当前代码无效。

TextboxTheme.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="ModernTextbox">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBox}">
                    <Border CornerRadius="10" Background="#353340" Width="200" Height="30">
                        <Grid>
                            <Rectangle StrokeThickness="1"/>
                            <TextBox Margin="1" BorderThickness="0" Background="Transparent" VerticalContentAlignment="Center" Padding="5" Foreground="#CFCFCF" x:Name="SearchBox"/>
                            <TextBlock IsHitTestVisible="False" Text="{TemplateBinding ToolTip}" 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>
</ResourceDictionary>

对应View(SettingsView.xaml)中TextBox的例子:

<TextBox Text="{Binding DailyCalories}" ToolTip="Napi kalóriamennyiség" Width="250" Height="30" VerticalContentAlignment="Center" HorizontalAlignment="Center" Margin="2" Style="{StaticResource ModernTextbox}"/>

viewmodel中的属性:

private String _dailyCalories;
public String DailyCalories
{
    get { return _dailyCalories; }
    set
    {
        if (_dailyCalories != value)
        {
            _dailyCalories = value;
            OnPropertyChanged();
        }
    }
}

App.xaml:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Theme/TextboxTheme.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

您错过了在模板中绑定 Text 属性。

就这么简单

<TextBox
  x:Name="SearchBox"
  Margin="1"
  Padding="5"
  VerticalContentAlignment="Center"
  Background="Transparent"
  BorderThickness="0"
  Foreground="#CFCFCF"
  Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Text, UpdateSourceTrigger=PropertyChanged}" />