用户控制丢失数据 [英] User control lose data
问题描述
嗨我正在尝试开发Datalogger,所以我创建一个菜单,切换到选项我决定使用DataTemplates和不同的ViewModels。
问题是当我更改菜单时,前一菜单的
内容未保存,即当我选择菜单"lista"时并填写数据网格,当我再次选择菜单"lista"时,我转到另一个菜单。数据丢失。我不知道我需要添加什么,或者更改以便
数据不会丢失
MainWindows.xaml.cs
private void Novoteste_btn_Click(object sender, RoutedEventArgs e)
{
DataContext = NovoTesteVM.NovoTesteViewModel;
}
private void Grafico_btn_Click(object sender, RoutedEventArgs e)
{
DataContext = GraficoVM.Grafico;
}
NovoTesteVM.cs
在课堂上建立一个属性
public class NovoTesteVM
{
private static NovoTesteVM novoTesteViewModel;
public static NovoTesteVM NovoTesteViewModel
{
get
{
novoTesteViewModel = novoTesteViewModel ?? new NovoTesteVM();
return novoTesteViewModel;
}
}
}
MainWindows.xaml
这是如何绑定内容
This how a binding the content
<DataTemplate x:Name="NovotesteVtemplate" DataType="{x:Type viewmodels:NovoTesteVM}">
<view:NovoTesteV DataContext="{Binding }"/>
</DataTemplate>
< ContentControl Grid.Row =" 1" Grid.Column = QUOT 1 QUOT; Grid.ColumnSpan = QUOT; 4英寸Grid.RowSpan = QUOT; 5英寸Content =" {Binding}"
<ContentControl Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="4" Grid.RowSpan="5" Content="{Binding}"/>
< span style ="color:#242729; font-family:Arial,'Helvetica Neue',Helvetica,sans-serif; font-size:15px">
推荐答案
嗨Joao ,
Hi Joao,
根据您的描述,您希望在不同的集合中选择不同的datatemplate,我建议您可以使用数据模板选择器来执行此操作。
According to your description, you want to select different datatemplate when different collection, I suggest you can use Data Template Selector to do this.
以下是有关如何使用此文章的文章,您可以查看:
Here is the article about how to use this, you can take a look:
https:/ /www.wpftutorial.net/datatemplates.html
https://code.msdn.microsoft.com/windowsdes ktop / Using-Data-Template-f6dc23c8
最好的问候,
这篇关于用户控制丢失数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!