티스토리 뷰
반응형
[wpf/c#] mvvm light를 이용한 간단한 샘플 프로그램(소스)
#1 nuget package - mvvm light 설치
#2 ReceiverViewModel 구현
public class ReceiverViewModel : ViewModelBase
{
private string _contentText;
public string ContentText
{
get { return _contentText; }
set
{
_contentText = value;
RaisePropertyChanged("ContentText");
}
}
public ReceiverViewModel()
{
Messenger.Default.Register<ViewModelMessage>(this, OnReceiveMessageAction);
}
private void OnReceiveMessageAction(ViewModelMessage obj)
{
ContentText = obj.Text;
}
}
#SenderViewModel 구현
public class SenderViewModel : ViewModelBase
{
public SenderViewModel()
{
OnClickCommand = new RelayCommand(OnClickCommandAction, null);
}
private string _textBoxText;
public string TextBoxText
{
get { return _textBoxText; }
set
{
_textBoxText = value;
RaisePropertyChanged("TextBoxText");
}
}
public RelayCommand OnClickCommand { get; set; }
private void OnClickCommandAction()
{
var viewModelMessage = new ViewModelMessage()
{
Text = TextBoxText
};
Messenger.Default.Send(viewModelMessage);
}
}
#ViewModelLocator 등록
public class ViewModelLocator
{
public ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
SimpleIoc.Default.Register<SenderViewModel>();
SimpleIoc.Default.Register<ReceiverViewModel>();
}
public SenderViewModel SenderViewModel
{
get
{
return ServiceLocator.Current.GetInstance<SenderViewModel>();
}
}
public ReceiverViewModel ReceiverViewModel
{
get
{
return ServiceLocator.Current.GetInstance<ReceiverViewModel>();
}
}
public static void Cleanup()
{
}
}
#View와 연동 - SenderView
<UserControl x:Class="test_mvvm.Views.SenderView"
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:test_mvvm.Views"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:Command="http://www.galasoft.ch/mvvmlight"
mc:Ignorable="d"
DataContext="{Binding Source={StaticResource Locator}, Path=SenderViewModel}"
d:DesignHeight="400" d:DesignWidth="400"
Background="Moccasin"
>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Label Content="Sender Data" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="30" FontWeight="Bold" />
<TextBox Margin="10,0,0,0" Grid.Row="1" HorizontalAlignment="Left" Width="180" Height="30" Text="{Binding TextBoxText}" />
<Button Margin="0,10,10,10" Grid.Row="1" HorizontalAlignment="Right" Width="180" Content="Send" FontSize="30" FontWeight="Bold" Command="{Binding OnClickCommand}"/>
</Grid>
</UserControl>
#View와 연동 - ReceiverView
<UserControl x:Class="test_mvvm.Views.ReceiverView"
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:test_mvvm.Views"
mc:Ignorable="d"
Background="AliceBlue"
d:DesignHeight="300" d:DesignWidth="300" DataContext="{Binding Source={StaticResource Locator}, Path=ReceiverViewModel}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Label Content="Receiver Data" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="30" FontWeight="Bold" />
<Label Grid.Row="1" Content="{Binding ContentText}" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="YellowGreen" FontSize="50" FontWeight="Bold" />
</Grid>
</UserControl>
#View와 연동 - MainView
<Window x:Class="test_mvvm.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:test_mvvm"
mc:Ignorable="d"
xmlns:view="clr-namespace:test_mvvm.Views"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="400" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<view:ReceiverView Grid.Row="0" Grid.Column="0" />
<view:SenderView Grid.Row="0" Grid.Column="1"/>
</Grid>
</Window>
#Message 구현
public class ViewModelMessage : MessageBase
{
public string Text { get; set; }
}
이 후 테스트 진행
바인딩이 아주 잘된다.
#풀소스 경로
https://github.com/gofogo2/test_mvvm.git
#wpf #mvvm #mvvm light #gala #c# #window #desktop #mvc
반응형
'c#' 카테고리의 다른 글
[c#/wpf] UpdateSourceTrigger=PropertyChanged (mvvm 사용 시 바인딩 객체값이 변경되면 바로 반영하기) (0) | 2022.02.24 |
---|---|
[wpf/c#] mvvm light, CommandParameter 사용법 (0) | 2022.02.24 |
[wpf/c#] MVVM 기본 개념 및 설명 (소스 기반) (0) | 2022.02.24 |
[wpf/c#/unity] Wake On Lan 사용하기(원격부팅) (0) | 2022.02.16 |
[C#/wpf/unity] ftp 라이브러리 (0) | 2022.02.15 |
댓글
반응형