머리말
Prism의 기본 바인딩 규칙은 Views 폴더(예: Test.xaml)에 있는 인터페이스를 통해 ViewModels 폴더에서 해당 ViewModel(예: TestViewModel.xaml)을 찾는 것이므로 ViewModel의 접미사가 정확해야 합니다. 기본 규칙을 수정하려면 앱 재정의로 이동하세요. ConfigureViewModelLocator
사용자 정의 ViewModel을 바인딩해야 하는 경우 ConfigureViewModelLocator 메서드에 코드를 추가하세요.
//Test为自定义的ViewModel类
ViewModelLocationProvider.Register<MainWindow, Test>();
1. 데이터 바인딩
Views 및 ViewModels 폴더를 각각 생성하고 양식 및 ViewModel 클래스
xaml 코드를 생성하고 TextBox 텍스트
prism:ViewModelLocator.AutoWireViewModel을 바인딩합니다. True인 경우 자동으로 ViewModel을 연결한다는 의미입니다.
<Window x:Class="PrismTestDemo.Views.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
Title="{Binding Title}" Height="350" Width="525" >
<Grid>
<TextBox Width="120" Height="30" Text="{Binding NameText}"></TextBox>
</Grid>
</Window>
ViewModel 코드:
Prism Template Pack 확장을 설치하면 속성 정의를 단순화할 수 있습니다.propp
public class MainWindowViewModel : BindableBase
{
private string _nameText;
public string NameText
{
get {
return _nameText; }
set {
SetProperty(ref _nameText, value); }
}
public MainWindowViewModel()
{
this.NameText = "Hello Prism";
}
}
2. 주문
1. 간단한 명령
Prism은 DelegateCommand 유형을 사용하여 명령을 정의
하고 xaml에 버튼을 추가하여 명령을 실행합니다.
<Window x:Class="PrismTestDemo.Views.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
Height="350" Width="525" >
<StackPanel VerticalAlignment="Center">
<TextBox Width="120" Height="30" Text="{Binding NameText}"></TextBox>
<Button Width="120" Height="30" Command="{Binding ButtonCommand}">按钮</Button>
</StackPanel>
</Window>
뷰모델 코드:
public class MainWindowViewModel : BindableBase
{
private string _nameText;
public string NameText
{
get {
return _nameText; }
set {
SetProperty(ref _nameText, value); }
}
private DelegateCommand _buttonCommand;
public DelegateCommand ButtonCommand =>
_buttonCommand ?? (_buttonCommand = new DelegateCommand(ExecuteButtonCommand, CanExecuteButtonCommand));
public MainWindowViewModel()
{
this.NameText = "Hello Prism";
}
void ExecuteButtonCommand()
{
this.NameText = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
private bool CanExecuteButtonCommand()
{
return true;
}
}
CanExecuteButtonCommand는 이 명령이 실행 가능한지 여부를 나타냅니다. 반환된 부울 값은 기본적으로 명령이 속한 컨트롤의 IsEnable 속성에 바인딩됩니다.
Prism 템플릿 팩 확장을 설치하면 명령 생성이 단순화될 수 있습니다.cmd
2. 매개변수를 사용한 명령
xaml에 버튼을 추가하고 명령 매개변수를 전달합니다.
<Button Width="120" Height="30" Command="{Binding ParamCommand}" CommandParameter="我是参数">带参命令</Button>
뷰모델 코드:
private DelegateCommand<string> _paramCommand;
public DelegateCommand<string> ParamCommand =>
_paramCommand ?? (_paramCommand = new DelegateCommand<string>(ExecuteParamCommand));
void ExecuteParamCommand(string parameter)
{
MessageBox.Show(parameter);
}
빠른 명령을 사용하여 cmdg
만들기
3. 명령에 대한 이벤트
ICommandSource 인터페이스를 상속하는 컨트롤에만 명령 종속성 속성이 있습니다. 기본적으로 ButtonBase 및 MenuItem만 ICommandSource를 상속하므로 Button, RadioButton 및 CheckBox는 모두 Command 속성을 가지지만 일반적인 TextBox, ComboBox 등에는 Command 속성이 없습니다. .. 그렇다면 이 경우 명령을 어떻게 바인딩해야 할까요?
먼저 xaml에 다음 dll을 소개합니다.
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
SelectionChanged 이벤트를 ComboBox 컨트롤에 바인딩하고 다음과 같이 작성합니다.
<ComboBox x:Name="cmb" ItemsSource="{Binding DataList}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding SelChangedCommand}" CommandParameter="{Binding ElementName=cmb,Path=SelectedItem}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
뷰모델 코드:
private DelegateCommand<object> _selChangedCommand;
public DelegateCommand<object> SelChangedCommand =>
_selChangedCommand ?? (_selChangedCommand = new DelegateCommand<object>(ExecuteSelChangedCommand));
void ExecuteSelChangedCommand(object parameter)
{
MessageBox.Show(parameter?.ToString());
}