gpt4 book ai didi

wpf - 使用comboBox作为源将自动生成的DataGrid分组

转载 作者:行者123 更新时间:2023-12-03 10:53:46 24 4
gpt4 key购买 nike

ViewModel :
我想选择所有Persons(每个人都可以是Type:Employee,Manager或Customer ,是PersonType 中存在的Persons的不同集合)
这是我要让ViewModel进行的唯一操作。

View :
我想要一个ComboBoxPersonTypes和一个基于所选DataGridPersonType(换句话说,Id喜欢让ComboBox负责ComboBox DataGrid)过滤的Grouping,并且将在XAML中完成所有操作。

有什么建议?

谢谢你。

最佳答案

    <Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="250"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"/>
</Grid.ColumnDefinitions>
<ComboBox x:Name="combo" ItemsSource="{Binding PersonTypes}" Tag="{Binding Persons}"/>
<DataGrid Grid.Row="1" AutoGenerateColumns="False">
<DataGrid.ItemsSource>
<MultiBinding Converter="{StaticResource conv}">
<Binding Path="Tag" ElementName="combo"/>
<Binding Path="SelectedItem" ElementName="combo"/>
</MultiBinding>
</DataGrid.ItemsSource>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}"/>
</DataGrid.Columns>
</DataGrid>

</Grid>

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModel();
}
}
public class ViewModel
{
public ViewModel()
{
PersonTypes = new ObservableCollection<PersonType>() { PersonType.Manager, PersonType.Customer, PersonType.Employee };

Persons = new ObservableCollection<Person>();
Persons.Add(new Person() { Name = "ABC", Type = PersonType.Manager });
Persons.Add(new Person() { Name = "DEF", Type = PersonType.Manager });
Persons.Add(new Person() { Name = "GHI", Type = PersonType.Customer });
Persons.Add(new Person() { Name = "JKL", Type = PersonType.Manager });
Persons.Add(new Person() { Name = "MNO", Type = PersonType.Employee });
}
public ObservableCollection<Person> Persons { get; set; }
public ObservableCollection<PersonType> PersonTypes { get; set; }

}
public class Person
{
public string Name { get; set; }
public PersonType Type { get; set; }
}

public enum PersonType
{
Manager = 0,
Employee = 1,
Customer = 2
}
public class MyConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (values.Count() > 1 && values[1] !=null && values[0] is ObservableCollection<Person>)
{
string ptype = values[1].ToString();
ObservableCollection<Person> persons = (ObservableCollection<Person>)values[0];
if (ptype != null && persons != null)
{
return persons.Where(p => p.Type.ToString() == ptype).ToList();
}
}

return null;
}

public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}

我希望这将有所帮助。

关于wpf - 使用comboBox作为源将自动生成的DataGrid分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11663106/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com