属性未在文本框中更新 - WPF

时间:2014-02-08 13:55:16

标签: c# wpf binding

我将属性Message绑定到视图时遇到问题。 回调从WCF服务返回结果。我正在尝试将此结果分配给属性Message。我的文本框永远不会更新为新值 - 它始终显示TEST。

public class CallbackHandler : IDataExchangeCallback, INotifyPropertyChanged 
{
    public CallbackHandler()
    {
        this.Message = "TEST";
    }

    public void Result(string result)
    {
        Message = result; 
    }

    private string _message;
    public string Message
    {
        get { return _message; }
        set
        {
            _message = value;
            OnPropertyChanged("Message");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;

        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

<Window x:Class="guiClient.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:tb="http://www.hardcodet.net/taskbar"
        xmlns:local="clr-namespace:guiClient"
        Title="DataExchangeClient" Height="76" Width="297" WindowStyle="SingleBorderWindow" MinHeight="50" MinWidth="50" MaxWidth="300">
    <Window.DataContext>
        <local:CallbackHandler/>
    </Window.DataContext>
    <Grid>
        <TextBox HorizontalAlignment="Left" Height="45" TextWrapping="Wrap" VerticalAlignment="Top" Width="289" Text="{Binding Path=Message}"/>
    </Grid>
</Window>

这是接口:

------来自UserBuzzer

回调定义如下:

IDataExchangeCallback Callback
{
    get
    {
        return OperationContext.Current.GetCallbackChannel<IDataExchangeCallback>();
    }
}

界面:

// The callback interface is used to send messages from service back to client.
// The Result operation will return the current result after each operation.
public interface IDataExchangeCallback
{
    [OperationContract(IsOneWay = true)]
    void Result(string result);
}

2 个答案:

答案 0 :(得分:2)

原因可能是你没有在UI线程上提出PropertyChanged,因为你是从回调中调用它。尝试使用Dispatcher确保在UI线程上引发事件:

protected void OnPropertyChanged(string propertyName)
{
    PropertyChangedEventHandler handler = PropertyChanged;

    if (handler != null)
    {
        Application.Current.Dispatcher.Invoke(() =>
            handler(this, new PropertyChangedEventArgs(propertyName)));
    }
}

答案 1 :(得分:1)

我找到了解决方案。这非常糟糕,但是我不知道如何在UI线程上引发事件。

namespace guiClient
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, IDataExchangeCallback
    {
        public MainWindow()
        {
            InitializeComponent();
            Register();
        }

        public void Result(string result)
        {
            //this will not cause the application to hang
            Application.Current.Dispatcher.BeginInvoke(new Action(
              () => textBox.Text = result));
        }

        public void Register()
        {
            InstanceContext instanceContext = new InstanceContext(this);
            DataExchangeClient client = new DataExchangeClient(instanceContext);

            client.RegisterClient(Guid.NewGuid().ToString());
        }
    }
}

正如达米尔·阿尔提到的那样,我使用了遣派。在这种情况下,我命名为control并将结果传递给Text属性。

另请注意,MainWindow现在继承自IDataExchangeCallback。 这也很棘手:InstanceContext instanceContext = new InstanceContext(this);

如果有人知道如何在MVVM中实现这一点,请给我一个电话。

相关问题