WPF 可重用标签和文本框行

WPF reusable label and text box row

在我的应用程序中,我有一个包含很多行的表单
重复模式 :
标签和旁边的文本框。

   <Grid.ColumnDefinitions>
  
   </Grid.ColumnDefinitions>

我是 wpf 的新手,但有没有办法创建类似用户控件的东西,同时包含这两个控件?
每次我只是添加这个新控件并修改标签的内容。

当然有一种方法,叫做UserControl。只需右键单击您的项目并 select 添加新项目。然后浏览添加一个UserControl,这里是一个例子:

<UserControl x:Class="WpfApp.MyUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:local="clr-namespace:WpfApp"
             mc:Ignorable="d"
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="4*" />
            <ColumnDefinition Width="6*" />
        </Grid.ColumnDefinitions>

        <Label x:Name="lbl"  />
        <TextBox Grid.Column="1" Height="20" Width="100" />
    </Grid>
</UserControl>

然后,为了管理标签的内容,您将需要一个 dependency property,以便使用您的用户控件的任何内容都可以绑定到它(您也可以使用常规属性,但绑定将无法进行):

public partial class MyUserControl : UserControl
{
    public static readonly DependencyProperty LabelContentProperty = DependencyProperty.Register(
        "LabelContent", typeof(string), typeof(MyUserControl), new PropertyMetadata(default(string),OnLabelContentChanged));

    private static void OnLabelContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var control = (MyUserControl) d;
        control.lbl.Content = e.NewValue;
    }

    public string LabelContent
    {
        get => (string) GetValue(LabelContentProperty);
        set => SetValue(LabelContentProperty, value);
    }

    public MyUserControl()
    {
        InitializeComponent();
    }
}

如果您不想使用依赖属性,那么您可以使用类似于以下内容的内容:

public partial class MyUserControl : UserControl
{
    public MyUserControl()
    {
        InitializeComponent();
    }

    public string LabelContent
    {
        get => lbl.Content as string;
        set => lbl.Content = value;
    }
}

然后就用它吧!

<Window x:Class="WpfApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:wpfApp="clr-namespace:WpfApp"
        mc:Ignorable="d"
        WindowStartupLocation="Manual"
        Title="MainWindow">
    <Grid>
        <wpfApp:MyUserControl LabelContent="Hi there!"/>
    </Grid>
</Window>