I made a sample to use UserControl.Xaml
, ResourceDictionary.xaml
and Converter.cs
from my WPF project which named MVVMDemo
in UseXamlFromOthers
.
The project directory of MVVMDemois as below:
Right click UseXamlFromOthers's references and add MVVMDemo
, then add below code in UseXamlFromOthers's App.xaml
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MVVMDemo;component/Styles/MyMVVMStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Add xmlns:mvvm="clr-namespace:MVVMDemo;assembly=MVVMDemo"
and xmlns:sys="clr-namespace:System;assembly=mscorlib"
in UseXamlFromOthers's Xaml, then use code from MVVMDemo in UseXamlFromOthers like below:
<Window.Resources>
<mvvm:PersonViewModel x:Key="VM"></mvvm:PersonViewModel>
<mvvm:Converter x:Key="conv"></mvvm:Converter>
</Window.Resources>
<StackPanel>
<StackPanel Background="Azure">
<Label Content="Use UserControl from MVVMDemo" />
<mvvm:UserControl1 Width="300" Height="200" DataContext="{StaticResource VM}"></mvvm:UserControl1>
</StackPanel>
<StackPanel Background="LightGreen">
<Label Content="Use DictionaryStyle from MVVMDemo" />
<Button Name="btn" Content="Use Dictionary MVVMDemo " Style="{StaticResource StyleA}" Width="200" Height="30"></Button>
</StackPanel>
<StackPanel Background="LightPink">
<Label Content="Use Converter from MVVMDemo" />
<TextBox Margin="4" Text="{Binding Source={x:Static sys:DateTime.Now}, Converter={StaticResource conv},Mode=OneWay}" Width="200"/>
</StackPanel>
</StackPanel>
Result picture is:
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.