c# - Trying to understand how i can bind a WPF DP in a different class -
i trying wpf develop tiny scoreboard.
in project have 3 xaml files.
controldisplay.xaml : here set points team 1 , team 2 in scoreboard. right have 1 textbox scoreboard title.
layout1.xaml : first layout, contains title now.
layout2.xaml : second layout, same above, contains title.
my idea following. update 1 singleton class has 1 property title. both layout1 , layout2's label title bind singleton class property title.
i created basic structure it.
controldisplay.xaml:
public partial class controldisplay : window { private iscoreboarddata _scoreboarddata; private layout1 _layout1; private layout2 _layout2; public controldisplay() { initializecomponent(); _scoreboarddata = simpleinjectorcontainer.container.getinstance<iscoreboarddata>(); } private void showlayout1(object sender, routedeventargs e) { _scoreboarddata.title = "test"; _layout1 = new layout1(); _layout1.show(); } private void showlayout2(object sender, routedeventargs e) { _scoreboarddata.title = "test"; _layout2 = new layout2(); _layout2.show(); } }
layout1.xaml.cs (layout2 copy of layout1 codewise, different class name)
public partial class layout1 : window { private iscoreboarddata _scoreboarddata; public layout1() { _scoreboarddata = simpleinjectorcontainer.container.getinstance<iscoreboarddata>(); initializecomponent(); } }
layout1.xaml
<window x:class="smallscoreboard.layout1" .... x:name="layoutone"> <stackpanel> <label datacontext="{binding elementname=layoutone}" content="{binding _scoreboarddata.title}" /> </stackpanel> </window>
scoreboarddata.cs
public scoreboarddata : iscoreboarddata { public string title { get; set; } }
this not work since don't register dependency property anywhere? how can register dependency property inside scoreboarddata class? or there better way solve this? want able add more layouts in future , hope don't have add base binding logic each , of layout(x).xaml.cs files.
update
this layout1.xaml file right now:
<window x:class="simple_scoreboard.layout1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="layout" height="500" width="800" scrollviewer.verticalscrollbarvisibility="hidden" scrollviewer.horizontalscrollbarvisibility="hidden" windowstyle="none" allowstransparency="true" resizemode="canresizewithgrip" x:name="layoutone" mouseleftbuttondown="dwindow_mouseleftbuttondown"> <stackpanel> <label content="{binding path=title, mode=onetime}" fontsize="30" horizontalalignment="center" horizontalcontentalignment="center" verticalalignment="center" verticalcontentalignment="center" margin="0,10,0,0" fontweight="bold"></label> <button content="button" click="button_click_1"/> </stackpanel> </window>
and layout1.xaml.cs
public partial class layout1 : window { public iscoreboarddata _scoreboarddata; public layout1() { initializecomponent(); _scoreboarddata = scoreboardcontainer.container.getinstance<iscoreboarddata>(); datacontext = _scoreboarddata; } private void dwindow_mouseleftbuttondown(object sender, mousebuttoneventargs e) { dragmove(); } private void button_click_1(object sender, routedeventargs e) { _scoreboarddata.title = "click change title"; } }
and scoreboarddata class:
class scoreboarddata : iscoreboarddata, inotifypropertychanged { private string _title; public string title { { return _title; } set { _title = value; if (propertychanged != null) propertychanged(this, new propertychangedeventargs("title")); } } #region inotifypropertychanged public event propertychangedeventhandler propertychanged; #endregion }
i think problem in binding private field _scoreboarddata; should make public property. better solution bind window datacontext.
in window constructor
public layout1() { _scoreboarddata = simpleinjectorcontainer.container.getinstance<iscoreboarddata>(); initializecomponent(); datacontext = _scoreboarddata; }
in xaml
<window x:class="smallscoreboard.layout1" .... x:name="layoutone"> <stackpanel> <label text="{binding title}" /> </stackpanel> </window>
this way have scoreboarddata window datacontext , bindings without explicitly specified source bind object.
update: scoreboarddata should implement inotifypropertychanged..
public class scoreboarddata :iscoreboarddata, system.componentmodel.inotifypropertychanged { public event propertychangedeventhandler propertychanged; private string _title; public string title { { return _title; } set { _title = value; if(propertychanged!=null) propertychanged(this,new propertychangedeventargs("title")); } } }
Comments
Post a Comment