admin管理员组文章数量:1289529
I need my SharedViewModel to be set in design time to display data in my UI
as I run the app, I see data on the screen, because OnLoaded runs LoadStuffs
LoadStuffs initiates the SharedViewModel and creates data
but at design time after a rebuild, nothing show up in the designer
<Window x:Class="MyApp.MainWindow"
mc:Ignorable="d"
xmlns:local="clr-namespace:MyApp"
...
Loaded="OnLoaded"
d:DataContext="{Binding RelativeSource={RelativeSource Self}, Path=SharedViewModel}"
>
At runtime, these get executed
void OnLoaded(object sender, RoutedEventArgs e)
{
LoadStuffs();
}
private void LoadStuffs()
{
Logger.Log("LoadStuffs() executed!");
...
SharedViewModel = ...
}
The message "LoadStuffs() executed!" only appears at runtime, never in design
The property of the main class :
private SharedViewModel m_sharedViewModel;
public SharedViewModel SharedViewModel
{
set
{
m_sharedViewModel= value;
}
get
{
Logger.Log("SharedViewModel getter"); <<<<< this is never called as the log.txt is only created at runtime
if (m_sharedViewModel == null)
{
Logger.Log("Initializing DesignSharedViewModel");
LoadStuffs(); // Initializes m_sharedViewModel
}
return m_sharedViewModel;
}
}
Thanks for your help on this
I need my SharedViewModel to be set in design time to display data in my UI
as I run the app, I see data on the screen, because OnLoaded runs LoadStuffs
LoadStuffs initiates the SharedViewModel and creates data
but at design time after a rebuild, nothing show up in the designer
<Window x:Class="MyApp.MainWindow"
mc:Ignorable="d"
xmlns:local="clr-namespace:MyApp"
...
Loaded="OnLoaded"
d:DataContext="{Binding RelativeSource={RelativeSource Self}, Path=SharedViewModel}"
>
At runtime, these get executed
void OnLoaded(object sender, RoutedEventArgs e)
{
LoadStuffs();
}
private void LoadStuffs()
{
Logger.Log("LoadStuffs() executed!");
...
SharedViewModel = ...
}
The message "LoadStuffs() executed!" only appears at runtime, never in design
The property of the main class :
private SharedViewModel m_sharedViewModel;
public SharedViewModel SharedViewModel
{
set
{
m_sharedViewModel= value;
}
get
{
Logger.Log("SharedViewModel getter"); <<<<< this is never called as the log.txt is only created at runtime
if (m_sharedViewModel == null)
{
Logger.Log("Initializing DesignSharedViewModel");
LoadStuffs(); // Initializes m_sharedViewModel
}
return m_sharedViewModel;
}
}
Thanks for your help on this
Share Improve this question edited Feb 21 at 10:22 phil asked Feb 20 at 20:19 philphil 527 bronze badges 8 | Show 3 more comments1 Answer
Reset to default 0The cleanest way of allocating a ViewModel
to a View
at both design-time and run-time is using the ViewModelLocator
pattern.
1 - Add a class to handle creation of ViewModels.
public class ViewModelLocator
{
public ViewModelLocator()
{
// include any IOC setup here if required.
}
public MainViewModel MainViewModel
{
get
{
return new MainViewModel();
// replace this with a call to the IOC container if any injected servies are required.
}
}
// repeat for any other ViewModel types required.
}
2 - Create an instance of this locator class in App.xaml, making it a application wide resouce.
<Application.Resources>
<local:ViewModelLocator x:Key="ViewModelLocator">
</Application.Resources>
3 - in each View, reference this locator instance as a source for its DataContext
<Window
x:Class="MyApp.MainView"
...
DataContext="{Binding Source={StaticResource ViewModelLocator}, Path=MainViewModel}" />
For a more detailed example including how to have different injected services for design-time and run-time usage, check out my blog post.
本文标签: WPFdDataContexthow to initiate data in design like at runtimeStack Overflow
版权声明:本文标题:WPF - d.DataContext - how to initiate data in design like at run-time? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741408985a2377117.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
xmlns:local
to the CLR namespace which contains your SharedViewModel class? Does that class have a public constructor which takes no parameters? – Soonts Commented Feb 20 at 20:48