简体   繁体   English

从另一个UserControl WPF修改UserControl的文本块

[英]Modify textblock of a UserControl from another UserControl WPF

i have a UserControl that contains a TextBox now i am loading another UserControl that contains a TextBlock .When the button is clicked, I want to assign value entered in TextBox to TextBlock of another control that is loaded. 我有一个包含TextBoxUserControl ,现在我正在加载另一个包含TextBlock UserControl 。单击该按钮时,我想将在TextBox输入的值分配给另一个加载的TextBoxTextBlock How can i do this ? 我怎样才能做到这一点 ?

Main UserControl 主用户控件

<UserControl x:Class="IntelliVentory.UserControls.CategoryControl"
         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:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
         mc:Ignorable="d" 
         d:DesignHeight="670" d:DesignWidth="1100">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto" />
            <RowDefinition Height="auto" />
            <RowDefinition Height="auto" />
        </Grid.RowDefinitions>  
        <TextBox Grid.Row="0" Name="CategoryNameBox" Width="350" />
        <Button Grid.Row="1" Click="AddCategoryFunc">Load Another Control</Button>
        <Grid Grid.Row="2" Name="CategoriesWraper"></Grid>
    </Grid>
</UserControl>  

Another UserControl 另一个用户控件

<UserControl x:Class="IntelliVentory.UserControlModules.CategoryModule"
         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:IntelliVentory.UserControlModules"
         xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
         mc:Ignorable="d" 
         d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <TextBlock Name="CategoryName" FontSize="12" FontWeight="Thin">Category Name Here</TextBlock>
    </Grid>
</UserControl>  

Main UserControl.cs Loading another UserControl. Main UserControl.cs加载另一个UserControl。

private void AddCategoryFunc(object sender, RoutedEventArgs e)
{
    UserControl categoryMod = new CategoryModule();
    CategoriesWraper.Children.Add(categoryMod);
}

You want to have something like 您想拥有类似的东西

categoryMod.CategoryNameValue = categoryControl.CategoryNameValue;

So you need to define two properties, one CategoryNameValue property with which you get the Text value of the TextBox, and one CategoryNameValue property with which you can set the Text property of the TextBlock. 所以,你需要定义两个属性,一个CategoryNameValue与你得到的文本框的文本值属性,一个CategoryNameValue属性,使用它可以设置 TextBlock的Text属性。

Define this property in the CategoryControl class, CategoryControl类中定义此属性,

public string CategoryNameValue { get { return CategoryNameBox.Text; }

And this in CategoryModule class, 在CategoryModule类中,

public string CategoryNameValue { set { CategoryName.Text = value; }

And you can start using them in your code. 您可以在代码中开始使用它们。

You can define them as Dependency Properties instead of plain CLR properties, and then look to use data binding . 您可以将它们定义为Dependency Properties而不是普通的CLR属性,然后使用data binding With data binding both user controls can be bound to the same data model so their values are synced automatically. 使用数据绑定,两个用户控件都可以绑定到相同的数据模型,因此它们的值将自动同步。

Edit: 编辑:

Turns out you can access a UserControl's child elements from outside as if they are public fields. 事实证明,您可以从外部访问UserControl的子元素,就像它们是公共字段一样。 That is, you can write code like this without having to define new properties 也就是说,您可以编写这样的代码而不必定义新的属性

CategoryModule categoryMod = new CategoryModule();
categoryMod.CategoryName.Text = CategoryNameBox.Text; 
CategoriesWraper.Children.Add(categoryMod);  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM