gpt4 book ai didi

c# - 将项目动态添加到 WPF 组合框,然后在某些事件上重置值

转载 作者:太空狗 更新时间:2023-10-30 01:05:24 25 4
gpt4 key购买 nike

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

我有一个类(class) - 地点主要属性为 -

地点名称

位置ID

我希望将此类绑定(bind)到 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”。我无法使用 tag 属性这样做。 (我搜索了一下,似乎我们没有像 ListItem 中那样的 value 属性)似乎每个人都建议我将它与一个类绑定(bind)并设置 DisplayMemberPath 和 SelectedValuePath。现在,如果我直接绑定(bind)我的 Location 类,我该如何插入 --Select One-- 项目。我可以通过创建一个虚拟对象并在绑定(bind)之前将其插入到我的列表中来做到这一点。但这是在 WPF 中工作的最佳方式吗?也许我缺少一种完全不同的方法。请指教。

提前致谢。 !

最佳答案

下面是如何使用更 MVVM 的方法来做到这一点:

View 模型:

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>

通常会使用命令而不是在后面的代码中调用重置方法。但由于您没有使用完整的 MVVM 方法,这就足够了。

关于c# - 将项目动态添加到 WPF 组合框,然后在某些事件上重置值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18831926/

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