【问题标题】:Update text in adorner on button click单击按钮时更新装饰器中的文本
【发布时间】:2011-08-09 07:26:05
【问题描述】:

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

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

 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>

如果需要其他代码或方法,请告诉我..

【问题讨论】:

    标签: wpf wpf-controls adorner adornerlayer adornerdecorator


    【解决方案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");
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多