티스토리 뷰
각 UI.xaml에서 DataContext를 설정하지 않고
Mappings.xaml에서 설정하고
DataContext에서 Binding 해서 쓰기
기본적인 메커니즘은 MainWindow에 띄울 UI를 갈아 끼우면서 MainWindow의 DataContext를 바꿔주는 느낌인듯?
1. Views, ViewModels 폴더를 만들고
2. Views/LoginUI.xaml (UserControl)
<UserControl x:Class="WpfApp12.Views.LoginUI"
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:WpfApp12.Views"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="500">
<Grid>
<TextBlock HorizontalAlignment="Left" Height="75" Margin="200,100,0,0"
TextWrapping="Wrap" VerticalAlignment="Top" Width="135" Background="#FFEA3434"
Text="{Binding Aaa}"
/>
</Grid>
</UserControl>
3. ViewModels/LoginViewModel
namespace WpfApp12.ViewModels
{
class LoginViewModel
{
public string Aaa { get; set; } = "777777777777";
}
}
여기까지 내가 보여줄 화면, 화면과 연결할 ViewModel을 만들었음
이제부터는 LoginUI.xaml에 LoginViewModel을 DataContext로 인식하도록 설정함
1. Mappings.xaml (Resource Dictionary)
여기서 LoginUI.xaml과 LoginViewModel을 연결
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dataContext="clr-namespace:WpfApp12.ViewModels"
xmlns:views="clr-namespace:WpfApp12.Views">
<DataTemplate DataType="{x:Type dataContext:LoginViewModel}">
<views:LoginUI />
</DataTemplate>
</ResourceDictionary>
2. App.xaml
여기서 Mappings.xaml 등록
<Application x:Class="WpfApp12.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp12"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/WpfApp12;component/Mappings.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
경로에 "component/"는 기본적으로 쓰는건가 봄
3. ViewModels/ShellViewModel.cs
using Microsoft.Toolkit.Mvvm.ComponentModel;
namespace WpfApp12.ViewModels
{
class ShellViewModel : ObservableObject
{
private object? _currentDataContext;
#region Properties
public object? CurrentDataContext
{
get => _currentDataContext;
set => SetProperty(ref _currentDataContext, value);
}
#endregion // Properties
}
}
4. App.xaml.cs
using System.Windows;
using WpfApp12.ViewModels;
namespace WpfApp12
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
ShellViewModel shellViewModel = new ShellViewModel();
shellViewModel.CurrentDataContext = new LoginViewModel();
var shellWindow = new MainWindow();
shellWindow.DataContext = shellViewModel;
shellWindow.ShowDialog();
}
}
}
이거 하고 나니깐.. 그냥 cs파일에서 DataContext를 정해주니까 xaml에서 안정해줘도 됐던거네;;
'C#' 카테고리의 다른 글
[C#]Command Binding 예시 (0) | 2024.02.27 |
---|---|
[C#]DataBinding 예시 (0) | 2024.02.26 |
[C#]의존성 주입 예시 (0) | 2024.02.21 |