4

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

我有一个类- 具有主要属性的位置-

地点名称

位置 ID

我希望将此类绑定到 WPF 中的组合框。我从数据库中获取位置列表。我需要在组合框中显示列表,第一个文本/值对为 ---Select One--- / -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属性)每个人似乎都建议我将它与一个类绑定并设置 DisplayMemberPath 和 SelectedValuePath。现在,如果我直接与我的Location类绑定,如何插入 --Select One- 项目。我可以通过创建一个虚拟对象并在绑定之前将其插入到我的列表中来做到这一点。但这是在 WPF 中工作的最佳方式吗?也许我缺少一种完全不同的方法。请指教。

提前致谢。!

4

1 回答 1

5

以下是使用更多 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 方法,这应该就足够了。

于 2013-09-16T16:27:00.873 回答