2

First, see this code in the MainWindow

    <Grid x:Name="LayoutRoot" MinWidth="900" MinHeight="650" RenderTransformOrigin="0.5,0.5">
        <local:RightSideContent x:Name="rightPanel"  Grid.Column="1"  Width="Auto" Height="Auto"/>

    </Grid>

I create a User Control name RightPanel and name it in MainWindow.xaml rightPanel

Example, in the User Control RightPanel has a TextBlock name textblock. Then, and I want to update the TextBlock and I am in MainWindow, I must call rightPanel.textblock.Text ="...".

So I think it is not a good way, because if I am in another Class, so I can't go back to MainWindow to update this textblock, and I can't call a method(static or non) to MainWindow or to RightPanel to update. Once more reason I think it isn't good, anytime you must interactive the MainWindow, instead I think we should send directly the message to RightPanel.

Please help me, thanks and forgive if my English isn't clear enough!

4

2 回答 2

3

您可以向RightSideContent用户控件添加一个依赖属性来处理文本。这将让您直接从您MainWindow的 xaml 绑定到它。

然后,RightSideContent用户控件可以将 绑定textblock.Text到该依赖项属性,显示那里的内容。

于 2013-04-16T17:36:57.607 回答
2

您可以创建一个名为“Text”的依赖属性,然后将其绑定到 MainWindow 的 DataContext 上的一个属性。

假设您没有遵循 MVVM 模式,您的 MainWindow.cs 中会有一些属性,即代码隐藏。例如:

private string _rightSideText = string.Empty;
public string RightSideText
{
    get { return _rightSideText; }
    set
    {
        _rightSideText = value;
        OnPropertyChanged("RightSideText");
    }
}

这假设您已INotifyPropertyChanged在 MainWindow 上实现。

然后,在您的 MainWindow XAML 中:

<Grid x:Name="LayoutRoot" MinWidth="900" MinHeight="650" RenderTransformOrigin="0.5,0.5">
    <local:RightSideContent Text="{Binding Path=RightSideText}" x:Name="rightPanel"  Grid.Column="1"  Width="Auto" Height="Auto"/>
</Grid>

这假设您已经添加了依赖属性。

完成后,您只需在想要更改文本时设置“RightSideText”即可。

于 2013-04-16T17:37:32.587 回答