单击按钮更新装饰器中的文本

时间:2011-04-20 08:27:38

标签: wpf wpf-controls adorner adornerlayer adornerdecorator

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

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

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

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

资源文件中的Adorner元素 -

<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>

如果需要额外的代码或方法,请告诉我。

1 个答案:

答案 0 :(得分:1)

您是否尝试过创建一些模型并将其推送到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");
        }
    }
}