【发布时间】:2015-04-22 23:55:33
【问题描述】:
我在使用 WPF 进行数据绑定时遇到问题。我有一个 Webservice(WCF) 到一个 Windows 服务应用程序和一个 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");
谢谢!
【问题讨论】:
标签: c# wpf xaml data-binding