我在WPF应用程序中有一个ListView控件和几个按钮。根据按钮单击,我想将不同类的不同结果集填充到列表视图中。所有这些不同的类都包含在一个类(ReportClass)中,如下所示。方式,我试图实现这一点是通过从代码隐藏更改listview的GridView和ItemSource。此代码几乎可以正常工作。因为列表视图结果会在按钮单击时发生更改,但仅显示类名而不是该类列表中的结果。
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>
答案 0 :(得分:1)
有充分的理由遵循MVVM模式将视图与行为分开。对于当前情况,最好为ListView实现触发式样式:
extern crate hwloc;
use hwloc::{Topology, ObjectType};
fn main() {
// Create a new Topology
let topology = Topology::new();
// Get all objects with type "Core"
let cores = topology.objects_with_type(&ObjectType::Core);
// Match on the returned Result and print the length if successful.
match cores {
Ok(c) => println!("There are {} cores on this machine.", c.len()),
Err(e) => panic!(format!("Could not load cores because of: {:?}", e))
}
}
并改变MainWindow的实施
<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>
答案 1 :(得分:0)
我发现了这个错误。 listview没有使用gridview更新的原因是因为Gridview实例&#34; g1&#34;和&#34;&#34; g1x&#34; 。我错了,我已经实例化了我已经&#34; g1&#34;和&#34; g1x&#34;为null。并且,在构造函数方法中再次实例化,它没有更新公开声明的实例。