c# - Unable to Render ContentControl inside Window -
i think missing simple here.
this want: want create mainwindowviewmodel instance mwvm. want associate instance mainwindow.
i want associate view instance viewmodel1 member of mwvm. thus, want pass hierarchy of instances viewmodel in view. far, unable see view.
i trying render custom contentcontrol inside wpf window. when put mainwindow.xaml entry point of application, don't see anything. when put view1.xaml entry point (in app.xaml), can see text "hello". missing here?
mainwindow.xaml
<window x:class="mimicview.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:mimicview" mc:ignorable="d" d:datacontext="{d:designinstance local:mainwindowviewmodel}" title="mainwindow" height="350" width="525"> <window.resources> <datatemplate datatype="{x:type local:viewmodel1}"> <local:view1/> </datatemplate> </window.resources> <grid> <contentcontrol content="{binding viewmodel1}"></contentcontrol> </grid>
mainwindowviewmodel.cs
namespace mimicview { class mainwindowviewmodel { public mainwindowviewmodel() { this.viewmodel1 = new viewmodel1(); } public viewmodel1 viewmodel1 { get; set; } } }
view1.xaml
<usercontrol x:class="mimicview.view1" 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:local="clr-namespace:mimicview" mc:ignorable="d" d:designheight="300" d:designwidth="300" d:datacontext="{d:designinstance local:viewmodel1}"> <textbox text="hello"/>
app.xaml
<application x:class="mimicview.app" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:mimicview" startupuri="mainwindow.xaml"> <application.resources> </application.resources>
set datacontext
property of window:
<window x:class="mimicview.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:mimicview" mc:ignorable="d" d:datacontext="{d:designinstance local:mainwindowviewmodel}" title="mainwindow" height="350" width="525"> <window.resources> <datatemplate datatype="{x:type local:viewmodel1}"> <local:view1/> </datatemplate> </window.resources> <window.datacontext> <local:mainwindowviewmodel /> </window.datacontext> <grid> <contentcontrol content="{binding viewmodel1}"></contentcontrol> </grid> </window>
this sets design time datacontext
:
d:datacontext="{d:designinstance local:mainwindowviewmodel}"
you should set actual datacontext
property:
<window.datacontext> <local:mainwindowviewmodel /> </window.datacontext>
wiki
Comments
Post a Comment