首页 文章

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

提问于
浏览
0

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

我目前正在做的是从我的资源中获取所需的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 回答

  • 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");
            }
        }
    }
    

相关问题