在ViewModel中模型属性的WPF绑定[MVVM]
问题描述:
Im在我的应用程序中使用多种语言。
我试图用MVVM模式来做到这一点,这对我来说是新的,所以我从CodeProject和Tutorialspoint上阅读了教程。在ViewModel中模型属性的WPF绑定[MVVM]
我的继承人项目结构:
Model
ConverterModel.cs
LanguageModel.cs
ViewModel
ConverterViewModel.cs
View
ConverterView.xaml
ConverterView.xaml.cs
MainWindow.xaml
MainWindow.xaml.cs
我创建了一个模型,所有的语言字符串,我需要:
internal class LanguageModel : INotifyPropertyChanged
{
//GUI Language
private string title;
private string inputBtn;
private string outputBtn;
private string convertBtn;
...
public string Title
{
get
{
return title;
}
set
{
if(title != value)
{
title = value;
RaisePropertyChanged("Title");
}
}
}
public string InputBtn
{
get
{
return inputBtn;
}
set
{
if (inputBtn != value)
{
inputBtn = value;
RaisePropertyChanged("InputBtn");
}
}
}
public string OutputBtn
{
get
{
return outputBtn;
}
set
{
if (outputBtn != value)
{
outputBtn = value;
RaisePropertyChanged("OutputBtn");
}
}
}
public string ConvertBtn
{
get
{
return convertBtn;
}
set
{
if (convertBtn != value)
{
convertBtn = value;
RaisePropertyChanged("ConvertBtn");
}
}
}
...
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string property)
{
if(PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
}
然后,我创建在该模型的实例我ViewModel并填写所需的数据:
internal class ConverterViewModel
{
internal Settings Settings{ get; set; }
ConverterModel Model { get; set; } //where the Data Fill Method is located
internal LanguageModel LanguageModel { get; set; }
String Title {get; set;} = "Banana"; //<-- this seems to work just fine
internal ConverterViewModel()
{
Model = new ConverterModel();
Settings = new Settings();
if(Settings.Language == "")
{
Settings.Language = "English";
//getLanguage
Settings.Save();
}
this.LanguageModel = Model.SetLanguageModel(Settings.Language);
}
数据填充方法在ConverterModel:
class ConverterModel
{
internal LanguageModel SetLanguageModel(string language)
{
switch (language)
{
case "English":
LanguageModel english = new LanguageModel()
{
Title = "TitleSomething",
InputBtn = "Inputfile",
OutputBtn = "Outputfile",
ConvertBtn = "Convert",
};
return english;
case "German":
LanguageModel german = new LanguageModel()
{
//Work in Progress
};
return german;
case "French":
LanguageModel french = new LanguageModel()
{
//Work in Progress
};
return french;
case "Italian":
LanguageModel italian = new LanguageModel()
{
//Work in Progress
};
return italian;
default: return null;
}
}
}
我有我的绑定设置是这样的。
主窗口:
<Window x:Class="dta2pain.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:dta2pain"
xmlns:view="clr-namespace:dta2pain.View"
xmlns:viewModel="clr-namespace:dta2pain.ViewModel"
mc:Ignorable="d"
ResizeMode="NoResize"
WindowStartupLocation="CenterScreen"
Title="{Binding Title}" //<-- works
Height="657" Width="910">
<Grid>
<Window.DataContext>
<viewModel:ConverterViewModel/>
</Window.DataContext>
<view:ConverterView x:Name="ConverterViewControl">
<view:ConverterView.DataContext>
<viewModel:ConverterViewModel/>
</view:ConverterView.DataContext>
</view:ConverterView>
</Grid>
ConverterView:
<UserControl x:Class="dta2pain.View.ConverterView"
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:dta2pain.View"
xmlns:viewModel="clr-namespace:dta2pain.ViewModel"
mc:Ignorable="d"
d:DesignHeight="657" d:DesignWidth="910"
Title = "{Binding Path = Title}">
<Grid>
<Menu HorizontalAlignment="Left" Height="20" VerticalAlignment="Top" Width="910" >
</Menu>
<Grid Margin="0,15,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="0.5*" />
<RowDefinition Height="1*"/>
<RowDefinition Height="0.75*"/>
<RowDefinition Height="1.5*"/>
<RowDefinition Height="0.25*"/>
</Grid.RowDefinitions>
<Image x:Name="LogoImg" Grid.Row="0" Source="../Images/mammutLogoTop1280.jpg" Margin="0"/>
<Grid Grid.Row="1" Margin="15,5,15,0">
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1.5*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="765*"/>
<ColumnDefinition Width="62*"/>
<ColumnDefinition Width="47*"/>
</Grid.ColumnDefinitions>
<TextBox x:Name="InputTxt" Template="{StaticResource TextBoxBaseControlTemplate}" Grid.Column="0" Grid.Row="0" Margin="0,0,5,0" Height="30" FontSize="14" VerticalContentAlignment="Center" />
<Button x:Name="InputBtn" Template="{StaticResource RoundCornerBtn}" Grid.Column="1" Grid.Row="0" Margin="5,0,0,0" Height="30" FontSize="14" Background="#FF006561" Foreground="White" Grid.ColumnSpan="2" Content="{Binding LanguageModel.InputBtn}"/>
<TextBox x:Name="OutputTxt" Template="{StaticResource TextBoxBaseControlTemplate}" Grid.Column="0" Grid.Row="1" Margin="0,0,5,0" Height="30" FontSize="14" VerticalContentAlignment="Center"/>
<Button x:Name="OutputBtn" Template="{StaticResource RoundCornerBtn}" Grid.Column="1" Grid.Row="1" Margin="5,0,0,0" Height="30" FontSize="14" Background="#FF006561" Foreground="White" Grid.ColumnSpan="2" Content="{Binding LanguageModel.OutputBtn}"/>
<Button x:Name="ConvertBtn" Template="{StaticResource RoundCornerBtn}" Grid.Row="2" Grid.ColumnSpan="3" Margin="0,7,0,6" FontSize="16" Background="#FF006561" Foreground="White" FontWeight="Bold" Content="{Binding LanguageModel.ConvertBtn}"/>
</Grid>
</Grid>
我不知道我是否未能在设定我的绑定来访问的语言模型 实例的属性或者RaisePropertyChanged()方法以错误的方式实现。 我试过设置我的绑定像这样Path= LanguageModel.Property
之前。 我试图把模型属性放在可观察列表中,并对其进行迭代,这也没有奏效。
此外,当我尝试调试程序并将鼠标悬停在LangaugeModel属性上时,调试器将关闭一个*异常。 现在从异常我猜我的程序被圈入某个地方,但我找不到在哪里。
我希望这是足够详细,这的确存在许多本地化的XAML标记(例如wpflocalizeextension.codeplex.com)项目,以了解我的问题。(第一篇文章)
答
注意。你不必自己做。
中的字符串位于RESX文件:
- 备用语言:filename.resx
- 德语:filename.de.resx
- 法语:filename.fr.resx
请注意,在XAML标记中存在执行本地化的项目(例如https://wpflocalizeextension.codeplex.com/)。你不必自己做。 –
我记得XAML只能在公共类中使用公共属性才能正常工作。尝试删除内部并公开设置。 – Atlasmaybe
只是要注意:你可以使用resx文件进行恶化 –