0

我创建了我的自定义装饰器,用灰色画布覆盖我的主窗口,中间有一个文本块,以在我处理其他窗口时显示一些状态文本。

我目前正在做的是从我的资源中获取所需的装饰器元素(即带有文本块的画布)并将其传递给我的视图构造函数中的装饰器,如下所示 -

 ResourceDictionary reportResourceDictionary = App.LoadComponent(new Uri("Resources/ReportResources.xaml", UriKind.Relative)) as ResourceDictionary;
 UIElement adornerElement = reportResourceDictionary["RefreshingReportAdorner"] as UIElement;
 mainWindowBlockMessageAdorner = new MainWindowBlockMessageAdorner(mainPanel, adornerElement);

但是我想在某些情况下更新文本块中的文本说如果我点击其他窗口中的某个按钮但是如何动态更新文本?

资源文件中的装饰元素-

<Grid x:Key="RefreshingReportAdorner">
        <Rectangle Fill="Gray"
                   StrokeThickness="1"
                   Stroke="Gray"
                   HorizontalAlignment="Stretch"
                   VerticalAlignment="Stretch"/>
        <Border BorderBrush="Black"
                BorderThickness="2"
                Background="White"
                HorizontalAlignment="Center"
                VerticalAlignment="Center">
            <TextBlock i18n:LanguageManager.VisualId="6"
                       Text="Some Text(Update dynamically)"                       
                       Padding="15,10,15,10"/>
        </Border>
    </Grid>

让我知道是否需要其他代码或方法..

4

1 回答 1

2

您是否尝试过创建一些模型并将其推送到 RefreshingReportAdorner 元素的 DataContext?

代码:

 var reportResourceDictionary = App.LoadComponent(new Uri("Resources/ReportResources.xaml", UriKind.Relative)) as ResourceDictionary;
 var adornerElement = reportResourceDictionary["RefreshingReportAdorner"] as FrameworkElement;
 var model = new Model(); 
 model.MyText = "Initial text";
 adornerElement.DataContext = model;
 mainWindowBlockMessageAdorner = new MainWindowBlockMessageAdorner(mainPanel, adornerElement);
 ... 
 model.MyText = "Text after click";

XAML:

        <TextBlock i18n:LanguageManager.VisualId="6"
                   Text="{Binding MyText}"                       
                   Padding="15,10,15,10"/>

模型:

public class Item : INotifyPropertyChanged
{
    private string _myText;
    public string MyText
    {
        get
        {
            return this._myText;
        }
        set
        {
            this._myText= value;
            this.OnPropertyChanged("MyText");
        }
    }
}
于 2011-04-20T10:35:18.580 回答