在 WPF 中创建自定义 UI 元素

JME*_*JME 6 c# wpf xaml

我有几个TextBoxesLabels在我的代码与下面的XAML代码来实现:

<DockPanel HorizontalAlignment="Right">
    <TextBlock Foreground="Black" Padding="0,0,10,0">Serial Number:</TextBlock>
    <TextBox Width="150" IsReadOnly="True" BorderBrush="Gainsboro" Height="20"></TextBox>
</DockPanel>
Run Code Online (Sandbox Code Playgroud)

我可以通过执行以下操作来减少一些复制的代码:

<DockPanel HorizontalAlignment="Right">
    <TextBlock Style="{StaticResource CstmTextBoxLbl}">Serial Number:</TextBlock>
    <TextBox Style="{StaticResource CstmTextBox}"></TextBox>
</DockPanel>
Run Code Online (Sandbox Code Playgroud)

但它仍然有些冗长。是否可以执行以下操作:

<controls:CstmTextBox Style="{StaticResource CstmTextBox}" LabelText="Serial Number:" Text=""/>
Run Code Online (Sandbox Code Playgroud)

CstmTextBox将实现任何所需XAML获得一次相同的视觉效果,而且我可以访问两个TextBlock文本和TextBox代码的文本。如

CstmTextBox textbox;
textbox.LabelText = "Serial Number:";
String some_text = textbox.Text;

textbox.Text = "....";
Run Code Online (Sandbox Code Playgroud)

alm*_*ulo 5

将 a 添加UserControl到您的解决方案中,然后复制要在其中重用的 XAML。

如果您将x:Name控件设置为UserControl...

<UserControl ... >
    <DockPanel HorizontalAlignment="Right">
        <TextBlock x:Name="SerialNumberTextBlock" Style="{StaticResource CstmTextBoxLbl}">Serial Number:</TextBlock>
        <TextBox x:Name="SerialNumberTextBox" Style="{StaticResource CstmTextBox}"></TextBox>
    </DockPanel>
</UserControl>
Run Code Online (Sandbox Code Playgroud)

...您将能够从外部(代码隐藏)访问它们,如下所示:

CstmTextBox textbox;
textbox.SerialNumberTextBlock.Text = "Serial Number:";
String some_text = textbox.SerialNumberTextBox.Text;

textbox.SerialNumberTextBox.Text = "....";
Run Code Online (Sandbox Code Playgroud)

但如果您为要公开的属性创建 DependencyProperties,那就更好了。您可以在 UserControl 的代码隐藏中定义这些属性:

public string Text
{
    get
    {
        return (string)GetValue(TextProperty);
    }
    set
    {
        SetValue (TextProperty, value);
    }
}

public static readonly DependencyProperty TextProperty = 
    DependencyProperty.Register("Text", typeof(string), typeof (CstmTextBox));
Run Code Online (Sandbox Code Playgroud)

然后将这些属性绑定到 UserControl 内的控件:

<UserControl x:Name="Root" ... >
    <DockPanel HorizontalAlignment="Right">
        <TextBlock x:Name="SerialNumberTextBlock" Style="{StaticResource CstmTextBoxLbl}"
                   Text="{Binding LabelText, ElementName=Root}" />
        <TextBox x:Name="SerialNumberTextBox" Style="{StaticResource CstmTextBox}"
                 Text="{Binding Text, ElementName=Root, Mode=TwoWay}" />
    </DockPanel>
</UserControl>
Run Code Online (Sandbox Code Playgroud)

这样您就可以直接从外部设置属性,无论是代码隐藏、XAML 还是绑定。


小智 4

UserControl 或 CustomControl 可以满足您的需求。这是 CustomControl 的代码。

C#:

public class CstmTextBox : Control
{
    public string LabelText
    {
        get
        {
            return (string)GetValue (LabelTextProperty);
        }
        set
        {
            SetValue (LabelTextProperty, value);
        }
    }

    public static readonly DependencyProperty LabelTextProperty =
        DependencyProperty.Register ("LabelText", typeof (string), typeof (CstmTextBox), new PropertyMetadata (string.Empty));

    public string Text
    {
        get
        {
            return (string)GetValue (TextProperty);
        }
        set
        {
            SetValue (TextProperty, value);
        }
    }

    public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register ("Text", typeof (string), typeof (CstmTextBox), new PropertyMetadata (string.Empty));
}
Run Code Online (Sandbox Code Playgroud)

XAML:

    <Style TargetType="{x:Type controls:CstmTextBox}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type controls:CstmTextBox}">
                    <DockPanel HorizontalAlignment="Right">
                        <TextBlock Foreground="Black" Padding="0,0,10,0" Text="{TemplateBinding LabelText}"/>
                        <TextBox Width="150" IsReadOnly="True" BorderBrush="Gainsboro" Height="20" Text="{TemplateBinding Text}"/>
                    </DockPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
Run Code Online (Sandbox Code Playgroud)

将样式添加到您的资源并使用新控件:

    <controls:CstmTextBox LabelText="abcde" Text="1234"/>
Run Code Online (Sandbox Code Playgroud)