WPF 工具包数据网格错误
我有一个带有 2 个选项卡的选项卡控件。每个选项卡的内容都绑定到 ListCollectionView,数据模板有一个显示项目的网格。问题是,如果您单击任何列进行排序,如果我选择其他选项卡并返回到第一个选项卡,则排序将被清除。这是一个已知的错误吗?
这是代码:
<Window x:Class="WpfApplication3.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
Title="Window1" Height="538" Width="1223">
<Window.Resources>
<DataTemplate x:Key="TestTemplate">
<toolkit:DataGrid ItemsSource="{Binding}" AutoGenerateColumns="false" IsReadOnly="True" >
<toolkit:DataGrid.Columns>
<toolkit:DataGridTextColumn Binding="{Binding IntVal}" Header="Number"></toolkit:DataGridTextColumn>
<toolkit:DataGridTextColumn Binding="{Binding StringVal}" Header="String"></toolkit:DataGridTextColumn>
</toolkit:DataGrid.Columns>
</toolkit:DataGrid>
</DataTemplate>
</Window.Resources>
<Grid>
<TabControl>
<TabItem Header="tab1" ContentTemplate="{StaticResource TestTemplate}" x:Name="a" Content="{Binding}"/>
<TabItem Header="tab2" ContentTemplate="{StaticResource TestTemplate}" x:Name="b" Content="{Binding}"/>
</TabControl>
</Grid>
隐藏代码:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
List<test> intt = new List<test>
{
new test { IntVal = 5, StringVal = "abc" },
new test { IntVal = 12, StringVal = "cc" },
new test { IntVal = 2, StringVal = "sdgc" },
new test{IntVal=64,StringVal="df"},
new test{IntVal=1234,StringVal="werw"},
new test{IntVal=14,StringVal="sdvvd"},
new test{IntVal=136,StringVal="aasr"}
};
List<test> intt2 = new List<test>
{
new test { IntVal = 5, StringVal = "abc" },
new test { IntVal = 12, StringVal = "cc" },
new test { IntVal = 2, StringVal = "sdgc" },
new test{IntVal=64,StringVal="df"},
new test{IntVal=1234,StringVal="werw"},
new test{IntVal=14,StringVal="sdvvd"},
new test{IntVal=136,StringVal="aasr"}
};
this.a.DataContext = new ListCollectionView(intt);
this.b.DataContext = new ListCollectionView(intt2);
}
public class test
{
public int IntVal { get; set; }
public string StringVal { get; set; }
}
}
i have a tab control with 2 tabs. the content of each tab is binded to a ListCollectionView and the data template has a grid showing the items. The problem is that if you click on any of the columns to sort, if I select the other tab and return to the first tab the sorting is cleared. Is it a known bug?
here is the code:
<Window x:Class="WpfApplication3.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
Title="Window1" Height="538" Width="1223">
<Window.Resources>
<DataTemplate x:Key="TestTemplate">
<toolkit:DataGrid ItemsSource="{Binding}" AutoGenerateColumns="false" IsReadOnly="True" >
<toolkit:DataGrid.Columns>
<toolkit:DataGridTextColumn Binding="{Binding IntVal}" Header="Number"></toolkit:DataGridTextColumn>
<toolkit:DataGridTextColumn Binding="{Binding StringVal}" Header="String"></toolkit:DataGridTextColumn>
</toolkit:DataGrid.Columns>
</toolkit:DataGrid>
</DataTemplate>
</Window.Resources>
<Grid>
<TabControl>
<TabItem Header="tab1" ContentTemplate="{StaticResource TestTemplate}" x:Name="a" Content="{Binding}"/>
<TabItem Header="tab2" ContentTemplate="{StaticResource TestTemplate}" x:Name="b" Content="{Binding}"/>
</TabControl>
</Grid>
code behid:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
List<test> intt = new List<test>
{
new test { IntVal = 5, StringVal = "abc" },
new test { IntVal = 12, StringVal = "cc" },
new test { IntVal = 2, StringVal = "sdgc" },
new test{IntVal=64,StringVal="df"},
new test{IntVal=1234,StringVal="werw"},
new test{IntVal=14,StringVal="sdvvd"},
new test{IntVal=136,StringVal="aasr"}
};
List<test> intt2 = new List<test>
{
new test { IntVal = 5, StringVal = "abc" },
new test { IntVal = 12, StringVal = "cc" },
new test { IntVal = 2, StringVal = "sdgc" },
new test{IntVal=64,StringVal="df"},
new test{IntVal=1234,StringVal="werw"},
new test{IntVal=14,StringVal="sdvvd"},
new test{IntVal=136,StringVal="aasr"}
};
this.a.DataContext = new ListCollectionView(intt);
this.b.DataContext = new ListCollectionView(intt2);
}
public class test
{
public int IntVal { get; set; }
public string StringVal { get; set; }
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为每次更改所选选项卡时 TabControl 都会销毁并重新创建其子项。这可以解释为什么不保留排序。
您可以查看 WPF Disciples Google Group 上的此讨论可获取有关如何解决该问题的一些信息。
I think that the TabControl destroy and recreate its child every time you change the selected tab. That would explain why the sort is not kept.
You can check out this discussion on the WPF Disciples Google Group to have some infos about how to fix that problem.
我遇到过类似的问题。我认为 wpf 对数据模板使用相同的 contentpresenter。在您的情况下,数据网格的实例是相同的,当您切换选项卡时只有数据会反弹。
我所做的是在代码隐藏中创建控件的实例并将其设置为 TabItem 的内容属性。
例如
希望这有帮助..
I had ran into a similar issue. I think wpf uses the same contentpresenter for data templates. In your case instance of the datagrid is the same, only data is rebound when you switch tabs.
What I did was to create an instance of the control in codebehind and set it as a content property of the TabItem.
e.g
hope this helps..