c# - WPF: Can't access the view. Datagrid is not refreshing with data in tabs -
i'm following how create tab-able content in wpf/c#? want each tab show datagrid. datagrid doesn't show , doesn't show data. when step code, see 0,1 being set.
mainwindow.xaml
<window x:class="mvvmdatainstances.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:viewmodel="clr-namespace:mvvmdatainstances" title="mainwindow" height="350" width="525"> <window.datacontext> <viewmodel:mainwindowviewmodel /> </window.datacontext> <grid> <grid.resources> <datatemplate x:key="contenttemplate" datatype="{x:type viewmodel:childviewmodel}"/> </grid.resources> <tabcontrol contenttemplate="{staticresource contenttemplate}" itemssource="{binding items}" /> </grid> </window> mainwindowviewmodel.cs
public observablecollection<childviewmodel> items { get; private set; } public mainwindowviewmodel() { items = new observablecollection<childviewmodel> {new childviewmodel(0), new childviewmodel(1)}; } childview.xaml
<usercontrol x:class="mvvmdatainstances.view" 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:viewmodel="clr-namespace:mvvmdatainstances" mc:ignorable="d" d:designheight="300" d:designwidth="300"> <usercontrol.datacontext> <viewmodel:childviewmodel/> </usercontrol.datacontext> <datagrid horizontalalignment="stretch" verticalalignment="stretch" itemssource="{binding grid, mode=twoway}" /> </usercontrol> childviewmodel.cs
public class childviewmodel : viewmodelbase { private observablecollection<childmodel> _grid; public observablecollection<childmodel> grid { { return _grid; } private set { _grid = value; onpropertychanged("grid"); } } public childmodel data { get; set; } public childviewmodel() { } public childviewmodel(int tabnumber) { data = new childmodel {a = tabnumber.tostring(cultureinfo.invariantculture)}; grid = new observablecollection<childmodel> {data}; } } childmodel.cs
public class childmodel { public string { get; set; } public string b { get; set; } } viewmodelbase.cs
public class viewmodelbase : inotifypropertychanged { public void onpropertychanged(string name) { if (propertychanged != null) { propertychanged(this, new propertychangedeventargs(name)); } } public event propertychangedeventhandler propertychanged; } i see 1 grid per tab. entry on first tab has value of 0 property a. entry of second tab has value of 1 property b.
i see when onpropertychanged called propertychanged null.
i can access datagrid if have in mainwindow.xaml
<grid> <tabcontrol itemssource="{binding items}"> <tabcontrol.itemtemplate> <datatemplate> <textblock text="{binding tabtitle}"/> </datatemplate> </tabcontrol.itemtemplate> <tabcontrol.contenttemplate> <datatemplate> <datagrid horizontalalignment="stretch" verticalalignment="stretch" autogeneratecolumns="true" itemssource="{binding grid}"/> </datatemplate> </tabcontrol.contenttemplate> </tabcontrol> </grid> but onpropertychanged null , don't see grid
<datatemplate x:key="contenttemplate" datatype="{x:type viewmodel:childviewmodel}"> <viewmodel:childview /> </datatemplate>
your datatemplate empty. question, want along these lines:
<grid> <grid.resources> <datatemplate x:key="contenttemplate" targettype="{x:type viewmodel:childviewmodel}"> <datagrid autogeneratecolumns="false" . . .> <datagrid.columns> <!-- column definitions here --> </datagrid.columns> </datagrid> </datatemplate> </grid.resources> <tabcontrol contenttemplate="{staticresource contenttemplate}" itemssource="{binding items}" /> </grid>
Comments
Post a Comment