我在 WPF 应用程序中有一个 ListView 控件和几个按钮。基于按钮单击,我想将不同类的不同结果集填充到 ListView 中。所有这些不同的类都包装在一个类(ReportClass)中,如下所示。我试图实现这一点的方法是通过从代码隐藏更改 GridView 和 listview 的 ItemSource 。此代码几乎可以工作..因为 ListView 结果会在单击按钮时发生变化,但仅显示类名称而不是该类列表中的结果。
P.S:我不是 XAML/WPF 方面的专家。但是,如果有更简单的解决方案......欢迎回答。谢谢
这是我的代码:
public class animalTypes1
{
public string type1name { get; set; };
}
public class animalTypes2
{
public string type2name { get; set; };
}
public class ReportClass
{
public List<animalTypes1> animaltype1 = new List<animalTypes1>();
public List<animalTypes2> animaltype2 = new List<animalTypes2>();
}
private ObservableCollection<ReportClass> _DynamicreportClass;
public ObservableCollection<ReportClass> _DynamicReportSetClass
{
get
{
if (_DynamicreportClass == null)
{
_DynamicreportClass = new ObservableCollection<ReportClass>();
}
return _DynamicreportClass;
}
}
public MainWindow()
{
InitializeComponent();
GridView g1 = new GridView();
g1.AllowsColumnReorder = true;
g1.ColumnHeaderToolTip = "mammals";
GridViewColumn g1c = new GridViewColumn();
g1c.DisplayMemberBinding = new Binding("type1name");
g1c.Header = "AnimalReport1Header";
g1c.Width = 120;
g1.Columns.Add(g1c);
GridView g1x = new GridView();
g1x.AllowsColumnReorder = true;
g1x.ColumnHeaderToolTip = "mammals";
GridViewColumn g1cx = new GridViewColumn();
g1cx.DisplayMemberBinding = new Binding("type2name");
g1cx.Header = "AnimalReport2Header";
g1cx.Width = 120;
g1x.Columns.Add(g1cx);
ReportClass allreports = new ReportClass();
allreports.animaltype1.Add(new animalTypes1() { type1name = "Mammals" });
allreports.animaltype2.Add(new animalTypes2() { type2name = "Reptiles" });
this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new System.Action(delegate ()
{
_DynamicReportSetClass.Add(allreports);
}));
private void btn1_click(object sender, RoutedEventArgs e)
{
ReportGrid.ItemsSource = _DynamicreportClass[0].animaltype1;
ReportGrid.View = g1;
}
private void btn_click2(object sender, RoutedEventArgs e)
{
ReportGrid.ItemsSource = _DynamicreportClass[0].animaltype2;
ReportGrid.View = g1x;
}
这是 XAML:
<Grid>
<WrapPanel Margin="0,0,0,125">
<StackPanel Orientation="Horizontal">
<Button Content="Checkbtn" Click="btn1_click"></Button>
</StackPanel>
<Button Content="Checkbtn" Click="btn_click2"/>
</WrapPanel>
<ListView
x:Name="ReportGrid"
Margin="0,199,0,0"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Stretch"
>
<ListView.View>
<GridView/>
</ListView.View>
</ListView>
</Grid>
最佳答案
有充分的理由遵循 MVVM 模式将 View 与行为分开。对于当前情况,最好为ListView实现触发样式:
<ListView
Margin="0,199,0,0"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Stretch">
<ListView.Style>
<Style TargetType="ListView">
<Setter Property="ItemsSource" Value="{Binding Source1}"/>
<Setter Property="View">
<Setter.Value>
<GridView AllowsColumnReorder="True" ColumnHeaderToolTip="mammals">
<GridViewColumn Header="AnimalReport1Header" Width="120" DisplayMemberBinding="{Binding type1name}"/>
</GridView>
</Setter.Value>
</Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding IsSource2}" Value="True">
<Setter Property="ItemsSource" Value="{Binding Source2}"/>
<Setter Property="View">
<Setter.Value>
<GridView AllowsColumnReorder="True" ColumnHeaderToolTip="mammals">
<GridViewColumn Header="AnimalReport2Header" Width="120" DisplayMemberBinding="{Binding type2name}"/>
</GridView>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ListView.Style>
</ListView>
并更改MainWindow的实现
public partial class MainWindow : Window, INotifyPropertyChanged
{
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged = delegate { };
protected void OnPropertyChanged(string property)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
#endregion
private ObservableCollection<animalTypes1> _source1 = new ObservableCollection<animalTypes1> {
new animalTypes1() { type1name = "Mammals" } };
public ObservableCollection<animalTypes1> Source1
{
get { return _source1; }
}
private ObservableCollection<animalTypes2> _source2 = new ObservableCollection<animalTypes2> {
new animalTypes2() { type2name = "Reptiles" } };
public ObservableCollection<animalTypes2> Source2
{
get { return _source2; }
}
private bool _isSource2;
public bool IsSource2
{
get { return _isSource2; }
set
{
if (value != _isSource2)
{
_isSource2 = value;
OnPropertyChanged("IsSource2");
}
}
}
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
private void btn1_click(object sender, RoutedEventArgs e)
{
IsSource2 = false;
}
private void btn_click2(object sender, RoutedEventArgs e)
{
IsSource2 = true;
}
}
关于c# - 使用多个 Gridview C# WPF 更新 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34680274/