WPF新手。
我正在尝试将我的模型绑定到我的UI。因此,在我的用户操作期间更改属性时,我希望该字段在我的UI上发生更新。
这是我的模特:
namespace WpfApplication1
{
public class model2
{
private static string myField2;
public static string MyField2
{
get { return myField2; }
set { myField2 = value; }
}
}
}
我的标记:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:model2 x:Key="mymodel"/>
</Window.Resources>
<Grid>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Source={StaticResource ResourceKey=mymodel}, Path=MyField2}"></TextBlock>
<Button Content="static test!" Click="Button_Click_1" />
</StackPanel>
</Grid>
</Window>
我的代码背后:
using System.Windows;
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
model2.MyField2 = "static!";
}
}
}
UI上的字段不会改变?
答案 0 :(得分:5)
您需要通知对UI的更改,以便可以使用新值进行更新。
在您的情况下,您希望通知静态属性更改,因此您需要一个静态事件。问题是INotifyPropertyChanged接口需要一个成员事件,所以你不能这样做。
你最好的方法是实现Singleton模式:
SCHEDULE P0015D24#EP_DAILY_1_P
DRAFT
ON RUNCYCLE SIMPLE1 07/31/2004,09/30/2006,02/10/2007
EXCEPT RUNCYCLE CALENDAR2 PEATOFF
PRIORITY 0
:
P0015D24#EP_EA012_BFR
P0015D24#EP_EA012_N
FOLLOWS EP_EA012_BFR
END
SCHEDULE P0015D24#EP_DAILY_2
ON RUNCYCLE RULE1 VALIDTO 10/02/2013 "FREQ=DAILY"
EXCEPT RUNCYCLE CALENDAR2 PEATOFF
EXCEPT RUNCYCLE CALENDAR3 PTDLY
EXCEPT RUNCYCLE CALENDAR5 TWS85OFF
EXCEPT RUNCYCLE EXCLUDE
01/20/2007,02/10/2007,03/10/2007,04/21/2007,05/12/2007,06/09/2007,07/21/2007,08/11/2007,
12/23/2007,12/24/2007,12/31/2007,01/19/2008,02/09/2008,01/01/2009,02/07/2009,05/22/2009,
05/25/2009,07/02/2009,07/03/2009,07/04/2009,07/05/2009,09/04/2009,09/05/2009,09/07/2009,
12/24/2009,12/25/2009,01/01/2010,03/20/2010,04/10/2010,09/06/2010,06/11/2011,12/24/2012
AT 0400
CARRYFORWARD
FOLLOWS P0015D24#PEAT_BKUP_CYE.@
FOLLOWS P0015D24#PEAT_BKUP_MNTH.@
FOLLOWS P0015D24#PEAT_BKUP_SAT.@
FOLLOWS P0015D24#PEAT_BKUP_SATNOV.@
FOLLOWS P0015D24#PEAT_RESTR_POST.@
FOLLOWS P0015M00#PEAT_BATCH_DONE.@
FOLLOWS P0015M00#PEAT_REBOOT.@
FOLLOWS P0015D24#PEAT_BKUP_POST.UNRESTRICT_FPR02
FOLLOWS P0015D24#PEAT_BKUP_POST_S.SWITCH_LOG
FOLLOWS P0015D24#PEAT_BKUP_POST_Z.SWITCH_LOG
:
P0015D24#EP_BATCH_DT
END
然后让你的属性成为一个成员属性并绑定如下:
namespace WpfApplication1
{
public class model2 : INotifyPropertyChanged
{
//private ctor so you need to use the Instance prop
private model2() {}
private string myField2;
public string MyField2
{
get { return myField2; }
set {
myField2 = value;
OnPropertyChanged("MyField2");
}
}
private static model2 _instance;
public static model2 Instance {
get {return _instance ?? (_instance = new model2();)}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName) {
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
代码背后:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:model2 x:Key="mymodel"/>
</Window.Resources>
<Grid>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Source={x:Static local:model2.Instance}, Path=MyField2}"/>
<Button Content="static test!" Click="Button_Click_1" />
</StackPanel>
</Grid>
</Window>
答案 1 :(得分:2)
使用Static扩展来绑定TextBlocks文本属性:
<TextBlock Text="{Binding Source={Static MyModel.MyField2}, Mode=TwoWay">
但是Property仍然必须引发PropertyChanged事件。我理解你使用静态字段的原因是能够从其他地方设置值。你有没有想过使用消息呢?查看MVVM Light工具包和信使。这将解耦两个组件
答案 2 :(得分:1)
我认为静态属性不是你想要使用的,从评论我可以推断你只是用来使你的程序工作。以下是完整的工作代码。
<强>的App.xaml 强>
删除代码StartupUri="MainWindow.xaml
,而不是在代码隐藏中实例化MainWindow
以提供DataContext
。
<强> App.xaml.cs 强>
此处我们将Model2
的对象指定为Window.DataContext
,然后显示该窗口。
public partial class App : Application
{
public App()
{
Model2 model = new Model2();
MainWindow window = new MainWindow();
window.DataContext = model;
window.Show();
}
}
<强> MainWindow.xaml.cs 强>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
//We get hold of `DataContext` object
var model = this.DataContext as Model2;
model.MyField2 = "Hello World";
}
}
<强>型号:强>
public class Model2 : INotifyPropertyChanged
{
private string _myField2;
public string MyField2
{
get { return _myField2; }
set
{
_myField2 = value;
OnPropertyChanged("MyField2");
}
}
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
<强> MainWindow.xaml 强>
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding MyField2}"></TextBlock>
<Button Content="static test!" Click="ButtonBase_OnClick" />
</StackPanel>
</Grid>
</Window>
我查了一下,确实有效!