WPF DataBinding与其他命名空间/类

时间:2015-04-22 23:55:34

标签: c# wpf xaml data-binding

我在使用WPF进行数据绑定时遇到问题。我有一个Web服务应用程序的Web服务(WCF)和用于控制服务的WPF应用程序。在WPF应用程序中,我构建了一个文本框,我想从WebService接收日志。

此时我可以从同一名称空间(WPF应用程序)发送新数据,但是当我使用我的数据类实例从(WCF应用程序)发送它时,它不反映文本框中的新数据。

这是我的代码:

  

MainWindow.xaml

...
   <Grid Name="grid" Margin="0,0,346.6,4">
       <TextBox Name="Log" Text="{Binding Path=LogText}" ScrollViewer.CanContentScroll="True" IsReadOnly="True" BorderThickness="0" Background="Transparent" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" Grid.Column="2" Margin="30.8,35,-325.8,0" Height="303" Grid.RowSpan="2" Width="295"/>
   </Grid>
...
  

MainWindow.xaml.cs

public MainWindow()
    {
                InitializeComponent();
                grid.DataContext = Logs.Instance;
                ...
    }

public class Logs : INotifyPropertyChanged
{
           private static Logs instance;

           private Logs() { }

           public static Logs Instance
           {
              get 
              {
                 if (instance == null)
                 {
                     instance = new Logs();
                 }
                 return instance;
              }
           }

            public event PropertyChangedEventHandler PropertyChanged;

            protected void Notify(string propName)
            {
                if (this.PropertyChanged != null)
                {
                    PropertyChanged(this,new PropertyChangedEventArgs(propName));
                }
            } 

            private string _LogText = "";

            public string LogText
            {
                get
                {
                    return _LogText;
                }
                set
                {
                    _LogText = value;
                    Notify("LogText");
                }
            }

            public void LogBinding(String text)
            {
                LogText = text + LogText;
            }
}
  

WCF Webservice发送文本调用(其他命名空间)

Using "THE NAMESPACE OF WPF APP";

Logs.Instance.LogBinding("Some Text");

谢谢!

1 个答案:

答案 0 :(得分:1)

从您的描述中可以看出,您有两个独立的应用程序,它们作为单独的进程运行。静态实例不会在进程间共享,即使它们属于同一个类。您需要使用某种形式的跨进程通信将数据从Windows服务传递到WPF应用程序。