簡體   English   中英

從任何地方更新Usercontrol中的TextBox.Text

[英]Update the TextBox.Text in Usercontrol from everywhere

我面臨的問題似乎反復出現,並且有很多解決方案,但對不起,我無法/無法在我的情況下提出申請。 讓我總結拓撲:我有一個主窗口( 主窗口 )具一格,並在底部的電池我把由我(UC_StatusMonitor)與用戶控件中的文本框(LBL_CONN_Message),我想用更新編寫的用戶控件操作結果(例如“ 已連接到設備X ”,“ 無法從Y讀取數據 ”,“ 缺少字段 ”等)。 這基本上是用來通知用戶是對還是錯。 我知道必須使用Dependancy解決此問題,但是我的實現無法正常工作(肯定我錯過了一些事情)。

讓我顯示代碼:

MainWindows.xaml

<Window x:Class="LUX.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:local="clr-namespace:LUX"
        mc:Ignorable="d"
        Title="LUX" Name ="MainForm" Height="600" Width="800"  MinHeight="600" MinWidth="800">

    <Border Padding="10">

        <Grid Name="Main_Grid">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="160"/>
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="100"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="22"/>
            </Grid.RowDefinitions>

            <!-- others controls ....   -->


            <!-- Monitor   -->
            <StackPanel Name="Stack_Monitor" Grid.Row="2" Grid.Column="1">
                <Grid Name="BottomBar" Height="20">
                    <local:UC_StatusMonitor Height="100" Grid.Row="2" Grid.Column="1"/>
                </Grid>
            </StackPanel>
        </Grid>



    </Border>
</Window>

然后是控件的代碼

UC_StatusMonitor.xaml

<UserControl x:Class="LUX.UC_StatusMonitor"
             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:LUX"
             mc:Ignorable="d" 
             xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
             d:DesignHeight="20" d:DesignWidth="300">
    <Grid Name="UCR_Base" Background="White">
        <WrapPanel>
            ...
            <TextBox Name="LBL_CONN_Message" Margin = "10 0 0 0" VerticalAlignment="Center" IsReadOnly="True" BorderThickness="0" Text="{Binding MyTextProperty, ElementName=control}"/>
            ...
        </WrapPanel>
    </Grid>
</UserControl>

UC_StatusMonitor.xaml.cs

namespace LUX {
    /// <summary>
    /// Interaction logic for StatusMonitor.xaml
    /// </summary>
    public partial class UC_StatusMonitor : UserControl
    {
        Button[] MenuButtons;

        //// The dependency property which will be accessible on the UserControl
        public static readonly DependencyProperty MyTextPropertyProperty = DependencyProperty.Register("MyTextProperty", typeof(string), typeof(UC_StatusMonitor), new UIPropertyMetadata(String.Empty));

        public string MyTextProperty
        {
            get { return (string)GetValue(MyTextPropertyProperty); }
            set { SetValue(MyTextPropertyProperty, value); }

因此,我期望在所有地方(在工作空間的所有類中,我都聲明一個狀態監視器靜態對象),我應該使用類似

MyTextProperty =“已連接”;

然后在文本框上看到此消息進入用戶控件。

顯然,不會發生:(

謝謝和最好的問候

我設法使UserControl與綁定一起使用。
這是我的操作方式:
UserControlWithBinding.xaml

<UserControl x:Class="SO_app.UserControlWithBinding"
         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:SO_app"
         mc:Ignorable="d" 
         d:DesignHeight="20" d:DesignWidth="300">
<Grid Name="IAmGroot"> <!-- Name this grid as you want as this is what we will use for internal Binding -->
    <TextBlock Text="{Binding Status}"/><!-- Because this grid is bound to the control it self you will always use this property, unless you rename it :-) -->
</Grid>


現在,在UserControlWithBinding.xaml.cs后面的代碼是:

/// <summary>
/// Interaction logic for UserControlWithBinding.xaml
/// </summary>
public partial class UserControlWithBinding : UserControl
{
    public UserControlWithBinding()
    {
        InitializeComponent();
        IAmGroot.DataContext = this;// this is where we set the data context for the grid so it uses the UserControl and not the inherited one. This will allow us to have the DataContext for User Control to be still inherited but the grid will look into a UserControl as an object to assign it to it's data context.
    }

    public string Status
    {
        get { return (string)GetValue(StatusProperty); }
        set { SetValue(StatusProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Status.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty StatusProperty =
        DependencyProperty.Register("Status", typeof(string), typeof(UserControlWithBinding), new PropertyMetadata(null));//note that I am using null and not string.Empty


}  



現在,讓我們在xaml中使用控件:

 <local:UserControlWithBinding Status="{Binding PropertyThatYouWantToBindThisControlsText}"/> 

這應該可以解決您的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM