在自定义控件库中单击Button时修改TextBox值

时间:2015-11-13 11:54:35

标签: c# wpf custom-controls

我在自定义控件库中创建了一个文本框和按钮,初始文本框值为"欢迎"单击按钮时,文本框文本值被修改为"嗨"。

Generic.Xaml:

    <ResourceDictionary
    x:Class="WpfCustomControlLibrary2.Themes.Class1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfCustomControlLibrary2">
    <Style TargetType="{x:Type local:CustomControl1}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:CustomControl1}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">

                        <WrapPanel HorizontalAlignment="Center" VerticalAlignment="Center">
                            <TextBox x:Name="txb" Text="Welcome" Width="50" Height="30" FontSize="15"/>
                            <Button Content="+" Width="35" Height="30" FontSize="15" Click="Button_Click_Add" />
                        </WrapPanel>

                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

的Class1.cs:

namespace WpfCustomControlLibrary2.Themes
{
    partial class Class1
    {
        private void Button_Click_Add(object sender, RoutedEventArgs e)
        {
            //What i do here  
        }
    }
}

MainWindow.Xaml

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfCustomControlLibrary2;assembly=WpfCustomControlLibrary2"
        Title="MainWindow" Height="350" Width="525">

    <Grid x:Name="grid">
        <local:CustomControl1 />
    </Grid>

</Window>

请帮助解决此问题,谢谢

1 个答案:

答案 0 :(得分:1)

你不能这样做,就像你这样做。 而且你也必须遵守名称规则。

试一试:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfCustomControlLibrary2;assembly=WpfCustomControlLibrary2">
    <Style TargetType="{x:Type local:CustomControl1}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:CustomControl1}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <WrapPanel HorizontalAlignment="Center" VerticalAlignment="Center">
                            <TextBox x:Name="PART_TextBox" Text="Welcome"/>
                            <Button x:Name="PART_Button" Content="+" />
                        </WrapPanel>

                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

[TemplatePart(Name = "PART_TextBox", Type = typeof(TextBox))]
[TemplatePart(Name = "PART_Button", Type = typeof(Button))]
public class CustomControl1 : Control
{
    static CustomControl1()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl1), new FrameworkPropertyMetadata(typeof(CustomControl1)));
    }

    public CustomControl1()
    {
    }

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        var button = GetTemplateChild("PART_Button") as Button;
        if (button != null)
        {
            button.Click += Button_Click;
        }
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        var textBox = GetTemplateChild("PART_TextBox") as TextBox;
        if (textBox != null)
        {
            textBox.Text = "HI!";
        }
    }
}
相关问题