我有產權更新MVVM屬性不更新
奇怪的問題我的模型看起來像
using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using TestSome.DataType;
using TestSome.MessageInfrastructure;
using TestSome.WorkingWithNode;
namespace TestSome.Model
{
class CentralModel
{
private BalanceTest someTest;
public BalanceTest SomeTest
{
get { return someTest; }
set { someTest = value; }
}
public void ListenBalance()
{
SocketHandler.Socket.Handle.Add("balance", (m) =>
{
try
{
BalanceTest bt = m.Message.Json.GetFirstArgAs<BalanceTest>();
SomeTest = bt;
Messenger.Default.Send(new BalanceCommunicator { TestBalance = bt }, "Token");
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.Message.ToString());
}
});
}
}
}
我的視圖模型看起來像
public class BalanceViewModel : ViewModelBase
{
private BalanceTest some;
public BalanceTest Some
{
get { return some; }
set { some = value; RaisePropertyChanged("Some"); }
}
private CentralModel CM;
public BalanceViewModel()
{
try
{
CM = new CentralModel();
CM.ListenBalance();
Some = CM.SomeTest;
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.Message.ToString());
}
}
和XAML看起來像
<UserControl x:Class="TestSome.Views.BalanceView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:Test="clr-namespace:TestSome.Model"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<Test:CentralModel x:Key="CentralMode"></Test:CentralModel>
</UserControl.Resources>
<Expander ExpandDirection="Right">
<StackPanel Orientation="Horizontal">
<Label Content="{Binding SomeTest.Balance,Source={StaticResource CentralMode},Mode=TwoWay}"></Label>
</StackPanel>
</Expander>
爲什麼不更新?
你初始化了DataContext嗎? – 2015-03-25 09:45:46
public BalanceView() { InitializeComponent(); DataContext = new BalanceViewModel(); } – A191919 2015-03-25 09:47:10
請勿任意將文本的每一位斜體。沒用的。 – 2015-03-25 09:51:34