C# 更改选项卡后,WPF TabControl和DataGrid组合框列返回初始位置
我有一个奇怪的问题,希望有人能帮我解决。 问题就在这里。 我有一个应用程序需要在一个主控件中动态创建C# 更改选项卡后,WPF TabControl和DataGrid组合框列返回初始位置,c#,wpf,xaml,mvvm,C#,Wpf,Xaml,Mvvm,我有一个奇怪的问题,希望有人能帮我解决。 问题就在这里。 我有一个应用程序需要在一个主控件中动态创建TabItem。 主窗口在堆栈面板中有两个按钮,还有一个选项卡控件(将承载不同的选项卡项实例: <DockPanel> <StackPanel Orientation="Horizontal" DockPanel.Dock="Top"> <Button Content="Tab1" Command="{Binding AddTab}" Comm
TabItem
。
主窗口在堆栈面板中有两个按钮
,还有一个选项卡控件
(将承载不同的选项卡项
实例:
<DockPanel>
<StackPanel Orientation="Horizontal" DockPanel.Dock="Top">
<Button Content="Tab1" Command="{Binding AddTab}" CommandParameter="Tab1"/>
<Button Content="Tab2" Command="{Binding AddTab}" CommandParameter="Tab2"/>
</StackPanel>
<TabControl DockPanel.Dock="Bottom"
ItemsSource="{Binding TabItems}"
SelectedItem="{Binding CurrentTabItem}"
ItemTemplate="{StaticResource TabItemItemTemplate}"/>
</DockPanel>
以及处理添加新选项卡的函数:
private void OnAddTabExecute(object obj)
{
var type = obj as string;
if (type != null)
{
TabItem ti;
if (type == "Tab1")
{
ti = new Tab1ViewModel();
}
else
{
ti = new Tab2ViewModel();
}
TabItems.Add(ti);
CurrentTabItem = ti;
}
在主窗口中,我声明了两个数据模板,它们负责将选定的“Tab1ViewModel”或“Tab2ViewModel”关联到相应的视图:
<DataTemplate DataType="{x:Type local:Tab1ViewModel}">
<local:Tab1View/>
</DataTemplate>
<DataTemplate DataType="{x:Type local:Tab2ViewModel}">
<local:Tab2View/>
</DataTemplate>
显示的属性在ViewModel中,所有内容都按照我的要求加载,但问题在于:
2017年3月1日编辑 我正在为
选项卡1viewmodel
提供代码:
public class Tab1ViewModel : TabItem
{
public ObservableCollection<Country> Countries { get; set; }
public ObservableCollection<Thing> ThisThings { get; set; }
public Thing ThisThing { get; set; }
//public NewerTabViewModel NewerTabViewModel { get; set; }
//public NewTabViewModel NewTabViewModel { get; set; }
public int SelectedIndex { get; set; }
public override string Header
{
get
{
return "Tab 1";
}
}
public Tab1ViewModel()
{
ThisThings = new ObservableCollection<Thing>();
ThisThings.Add(new Thing() { CountryID = 1, Name = "Greg" });
ThisThings.Add(new Thing() { CountryID = 2, Name = "John" });
ThisThings.Add(new Thing() { CountryID = 4, Name = "Sam" });
ThisThing = new Thing()
{
CountryID = 3,
Name = "Thingy",
//HisThings = ThisThings
};
//NewTabViewModel = new NewTabViewModel(ThisThing);
//NewerTabViewModel = new NewerTabViewModel(ThisThing);
Countries = new ObservableCollection<Country>();
Countries.Add(new Country() { ID = 1, Description = "Bulgaria" });
Countries.Add(new Country() { ID = 2, Description = "UK" });
Countries.Add(new Country() { ID = 3, Description = "USA" });
Countries.Add(new Country() { ID = 4, Description = "Russia" });
Countries.Add(new Country() { ID = 5, Description = "Germany" });
}
}
公共类Tab1ViewModel:TabItem
{
公共可观察收集国家{get;set;}
公共可观测集合ThisThisThings{get;set;}
公共事物ThisThing{get;set;}
//public NewerTabViewModel NewerTabViewModel{get;set;}
//公共NewTabViewModel NewTabViewModel{get;set;}
public int SelectedIndex{get;set;}
公共重写字符串头
{
得到
{
返回“表1”;
}
}
公共选项卡1ViewModel()
{
ThisThings=新的可观察集合();
Add(newthings(){CountryID=1,Name=“Greg”});
Add(newthing(){CountryID=2,Name=“John”});
添加(newthings(){CountryID=4,Name=“Sam”});
新事物
{
CountryID=3,
Name=“Thingy”,
//hithings=这件事
};
//NewTabViewModel=新的NewTabViewModel(ThisThing);
//NewerTabViewModel=新的NewerTabViewModel(ThisThing);
国家=新的可观测集合();
添加(新国家(){ID=1,Description=“保加利亚”});
添加(新国家(){ID=2,Description=“UK”});
添加(新国家(){ID=3,Description=“USA”});
添加(新国家(){ID=4,Description=“俄罗斯”});
添加(新国家(){ID=5,Description=“Germany”});
}
}
您的问题是:每次选择选项卡时都会创建一个新的选项卡项:
这个:
private TabItem _currentTabItem;
public TabItem CurrentTabItem {get;set;}
private TabItem _currentTabItem;
public TabItem CurrentTabItem
{
get {return _currentTabItem;}
set
{
if(_currentTabItem == value) return;
_currentTabItem = value;
OnPropertyChanged(nameOf(CurrentTabItem);
}
}
应该是:
private TabItem _currentTabItem;
public TabItem CurrentTabItem {get;set;}
private TabItem _currentTabItem;
public TabItem CurrentTabItem
{
get {return _currentTabItem;}
set
{
if(_currentTabItem == value) return;
_currentTabItem = value;
OnPropertyChanged(nameOf(CurrentTabItem);
}
}
ThisThings是一个集合?你能提供相关的来源吗?我怀疑你的SelectedValue=“{Binding CountryID}”绑定不起作用。嗨@Derrickmeller,我刚刚用初始化
ThisThings
集合的方式编辑了原始帖子,ThisThing对象和国家集合Hi@Nawed Nabi Zada,不,这不是解决方案。我已经尝试过了,它不会影响奇怪的行为。我将编辑原始帖子中的代码,所以不要把别人搞糊涂了。谢谢你的建议。