将项动态添加到 WPF 组合框,并在以后重置某些事件的值

本文关键字:事件 添加 动态 WPF 组合 | 更新日期: 2023-09-27 18:31:26

我是WPF的新手,但我搜索了很多,最后决定向你们寻求帮助...

我有一堂课 - 位置主要属性为 -

位置名称

位置标识

我希望将此类绑定到 WPF 中的组合框。我从数据库中获取位置列表。我需要在组合框中显示列表,第一个文本/值对为---选择一个---/-1。现在,到目前为止我已经做到了——

创造-

public ObservableCollection<ComboBoxItem> cbLocationList { get; set; }
cbLocationList = new ObservableCollection<ComboBoxItem>();
SelectedcbDefaultLocationListItem = new ComboBoxItem { Content = "---Select One---" , Tag="-1"};
cbLocationList.Add(SelectedcbDefaultLocationListItem);

循环中将项目填充为 -

foreach (Location loc in LocationtList)
{
 cbLocationList.Add(new ComboBoxItem  { Content = loc.LocationName, Tag=loc.LocationID.ToString() });
}

我在 XAML 中将 cbLocationList 设置为 -

ItemsSource="{Binding cbLocationList}" 

的组合框。这很好用,但是在重置表单时,我需要将组合框的值重置为"-1"。我无法使用标签属性执行此操作。(我搜索了一下,似乎我们没有像列表项那样的值属性)每个机构似乎都建议我用一个类绑定它,并设置DisplayMemberPath和SelectedValuePath。现在,如果我直接与我的 Location 类绑定,如何插入 --选择一个-- 项。我可以通过创建一个虚拟对象并在绑定之前将其插入到我的列表中来做到这一点。但这是在 WPF 中工作的最佳方式吗?也许我缺少一种完全不同的方法。请指教。

提前感谢。!

将项动态添加到 WPF 组合框,并在以后重置某些事件的值

以下是使用

更多 MVVM 方法执行此操作的方法:

视图模型:

public class MyViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    public Location SelectedcbDefaultLocationListItem = new Location { LocationName = "---Select One---", LocationID = -1 };
    public ObservableCollection<Location> LocationList { get; set; }
    private int _selectedLocationID;
    /// <summary>
    /// Get/Set the SelectedLocationID property. Raises property changed event.
    /// </summary>
    public int SelectedLocationID
    {
        get { return _selectedLocationID; }
        set
        {
            if (_selectedLocationID != value)
            {
                _selectedLocationID = value;
                RaisePropertyChanged("SelectedLocationID");
            }
        }
    }        
    /// <summary>
    /// Constructor
    /// </summary>
    public MyViewModel()
    {
        LocationList = new ObservableCollection<Location>();
        LocationList.Add(new Location() { LocationID = 1, LocationName = "Here" });
        LocationList.Add(new Location() { LocationID = 2, LocationName = "There" });
        LocationList.Insert(0, SelectedcbDefaultLocationListItem);
        SelectedLocationID = SelectedcbDefaultLocationListItem.LocationID;
    }
    /// <summary>
    /// Resets the selection to the default item.
    /// </summary>
    public void ResetSelectedItem()
    {
        SelectedLocationID = SelectedcbDefaultLocationListItem.LocationID;
    }
    private void RaisePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }   
}

代码隐藏:

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow
{
    public MyViewModel ViewModel { get; private set; }
    public MainWindow()
    {
        InitializeComponent();
        ViewModel = new MyViewModel();
        DataContext = ViewModel;
    }
    private void ResetButton_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        ViewModel.ResetSelectedItem();
    }
}

XAML:

<Window xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"  x:Class="StackOverflow.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:system="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <StackPanel>
            <ComboBox ItemsSource="{Binding LocationList, Mode=OneWay}" DisplayMemberPath="LocationName" SelectedValuePath="LocationID" SelectedValue="{Binding SelectedLocationID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
            <Button Click="ResetButton_Click" Content="Reset" Margin="5" HorizontalAlignment="Right" />
        </StackPanel>        
    </Grid>    
</Window>

通常,将使用命令,而不是在代码隐藏中调用 reset 方法。 但是,由于您没有使用完整的 MVVM 方法,这应该就足够了。