首页 文章

WPF4:如何在主窗口中设置UserControl中的值?

提问于
浏览
0

这必须很简单,至少它是旧的.Net,它可能需要四行代码 . 我在VS2010,C#,WPF4工作 .

我有一个带文本框的用户控件 . 当我单击主窗口中的按钮时,我希望我的用户控件文本框能够反映一些文本 . WPF4中是否可以使用少于500行的深奥代码?

问题是,虽然我知道文本框正在从用户控制代码中的断点获取新文本,但该文本永远不会反映到主窗口 . 主窗口仍显示原始文本 . 它必须是某种绑定的东西,我真的不认为我应该为这种简单的情况创建模板和资源 . 它必须是一些简单的东西,我忘了在WPF4的森林里 . 以下是我所拥有的 . 单击按钮后,文本框仍为空白;它没有说“你好地球人” .

在用户控制代码中:

public partial class UserControl1 : UserControl
{
    public static readonly DependencyProperty TextProperty;

    public UserControl1()
    {
        InitializeComponent();
    }

    static UserControl1()
    {
        TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(UserControl1), new UIPropertyMetadata(null));
    }

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

}

用户控制xaml:

<UserControl x:Class="WTFUserControlLibrary.UserControl1"
         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">
<Grid Height="164" Width="220">
    <TextBox Name="txtTest" BorderBrush="red" BorderThickness="2" Height="25" Text="{Binding ElementName=UserControl1, Path=Text, Mode=TwoWay}"></TextBox>
</Grid>

(我不知道在这种情况下文本绑定应该做什么 . )

主窗口代码:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        WTFUserControlLibrary.UserControl1 uc = new WTFUserControlLibrary.UserControl1();
        uc.Text = "hello earthlings";
    }
}

和主窗口xaml:

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:my="clr-namespace:WTFUserControlLibrary;assembly=WTFUserControlLibrary"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="71,65,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    <my:UserControl1 HorizontalAlignment="Left" Margin="71,94,0,0" Name="userControl11" VerticalAlignment="Top" Height="116" Width="244" />
</Grid>

感谢地球人(以及设计这个烂摊子的人!)

1 回答

  • 1

    在您的方法 button1_Click 中,您正在创建一个 new 用户控件 . 这不是窗口中的用户控件,永远不会显示 .

    相反,在XAML中为您的usercontrol指定一个名称:

    x:Name="uc"
    

    然后在 button1_Click 方法中,您只需删除创建新用户控件的第一行 .

    更新

    您希望用户控件XAML看起来更像这样:

    <UserControl x:Class="WTFUserControlLibrary.UserControl1"
             x:Name="thisControl"
             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">
    <Grid Height="164" Width="220">
        <TextBox Name="txtTest" BorderBrush="red" 
                 BorderThickness="2" Height="25" 
                 Text="{Binding ElementName=thisControl, Path=Text, Mode=TwoWay}" />
    </Grid>
    

    我将 x:Name="thisControl" 添加到根 UserControl 元素,然后在绑定中引用它 .

    我将尝试解释绑定:

    您在用户控件中有此文本框,但您希望能够将文本值绑定到用户控件之外的某些内容 . 您所做的是在用户控件上设置依赖项属性,并将该文本框绑定到该属性,因此您使用绑定基础结构将值从 UserControl 的顶层传递到其中的组成控件 .

    基本上,它看起来像这样:

    data
        ---> bind UserControl1.Text to data
             ---> bind TextBox.Text to UserControl1.Text
    

相关问题