【问题标题】:How to get bind Selected Item of my listview combobox's如何绑定我的列表视图组合框的选定项目
【发布时间】:2012-08-31 10:08:24
【问题描述】:

我对此感到非常困惑 - 如果答案真的很明显,我很抱歉,但我对编程很陌生。

我将用户控件设置为视图,该视图加载到我的主视图中的内容控件中。用户控件(称为 SetView)的数据上下文在 MainView 中设置。我可以愉快地将 SetView 绑定到 UserControlVM(称为 SetVM)。

在 SetVM 中,我加载了我创建的类的 ObservableCollection:

    public class WeightSet : Weights
{


    public string BodyArea { get; set; }
    public string ExerciseType { get; set; }
    public int SetNumber { get; set; }
    public static ObservableCollection<int> Reps { get; set; }


    #region Constructor


    //This is the main constructor
    public WeightSet(string bodyarea, string exerciseType, int setNumber)
    {
        BodyArea = bodyarea;
        ExerciseType = exerciseType;
        SetNumber = setNumber;
        Reps = new ObservableCollection<int>();
        AddReps();

    }

    #endregion Constructor

    #region Methods

    public void AddReps()
    {
        for (int i = 1; i < 100; i++)
        {
            Reps.Add(i);
        }
    }
    #endregion Methods

然后我的 SetView 有一个 ListView,其 ItemsSource 是

public ObservableCollection<WeightSet> Sets

这是 ListView 的 xaml:

<UserControl x:Class="CalendarTest.SetView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:forcombo="clr-namespace:CalendarTest.Model.Repository.Local_Data"
         xmlns:VM="clr-namespace:CalendarTest.ViewModel"
         mc:Ignorable="d" 
         d:DesignHeight="165" d:DesignWidth="300">
<Grid >
    <StackPanel>
    <StackPanel Orientation="Horizontal">
        <Label Content="{Binding CurrentExercise}" Width="100" Height="40"></Label>
        <Label Content="{Binding BodyArea}" Width="100" Height="40"></Label>

    </StackPanel>
        <ListView ItemsSource="{Binding Sets}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Set Number" DisplayMemberBinding="{Binding Path=SetNumber}" Width="100"></GridViewColumn>
                    <GridViewColumn Header="Select Reps" Width="120">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <ComboBox Width="100" ItemsSource="{Binding Source={x:Static forcombo:WeightSet.Reps }}" ></ComboBox>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>

                    </GridViewColumn>
                    <GridViewColumn DisplayMemberBinding="{Binding Path=Reps}"></GridViewColumn>
                </GridView>
            </ListView.View>


        </ListView>
    </StackPanel>

</Grid>

当我加载 SetView 时,我有一组数字列表和一个带有静态列表的组合框。这是一个镜头:

我似乎无法将组合框的选定项绑定回我的 ViewModel。有没有办法在 WeightSet 类中设置所选项目?我需要能够保存所选号码吗?

我知道我不能只绑定到一个属性,因为 ComboBox 的数量将由用户决定?对我当前设计的任何提示或更正将不胜感激

【问题讨论】:

    标签: c# wpf mvvm-light


    【解决方案1】:

    您可以在您的组合框上绑定SelectedItem 属性,并在您的 WeightSet 类中添加一个依赖属性

    xaml:

    <ComboBox ItemsSource="{Binding Source={x:Static forcombo:WeightSet.Reps }}"
              SelectedItem="{Binding SelectedItem}" />
    

    和属性

    public static readonly DependencyProperty SelectedItemProperty =
        DependencyProperty.Register("SelectedItem", typeof (int), typeof (WeightSet), new PropertyMetadata(default(int)));
    
    public int SelectedItem {
        get { return (int) GetValue(SelectedItemProperty); }
        set { SetValue(SelectedItemProperty, value); }
    }
    

    然后,当在组合框中选择一个值时,您的 Sets 实例中的 SelectedItem 将被更新。

    【讨论】:

    • 完美 - 非常感谢我所需要的。现在我在我的 VM 中收集的 WeightSets 最终得到了一个关于用户选择的 Rep 数字,这正是我想要的。看起来我需要更多地阅读依赖属性!
    • @Luthervd:是的,请记住,您需要扩展 DependencyObject 才能调用 GetValue 和 SetValue 方法。您可以通过创建一个新类 WeightSetViewModel 并让该方法“反映”您的 WeightSet 模型类来做到这一点。您的视图与视图模型对话,如果更新,视图模型会将信息提供给模型。这样,您可以在视图模型中拥有一堆“显示”属性,以免“污染”您的模型。如果您希望我扩展答案,请写评论。
    【解决方案2】:

    这里的关键是将组合框的 SelectedItem 依赖属性绑定到视图模型中合适的属性

    首先,您需要在您的 Weightset 类中实现 INotifyPropertyChanged

    接下来在 WeightClass 中引入一个名为 say SelectedRep 的属性,它看起来像

    int _selectedRep;
    ///<summary>Gets or sets SelectedRep.</summary>            
    public int SelectedRep
    {
        get { return _selectedRep; }
        set { _selectedRep = value; OnPropertyChanged("SelectedRep"); }
    }
    

    最后修改 Xaml 以将 ComboBox 的 SelectedItem 绑定到 SelectedRep 属性。

    <ComboBox Width="100" ItemsSource="{Binding Source={x:Static forcombo:WeightSet.Reps }}"  SelectedItem="{Binding SelectedRep, Mode=TwoWay}" />
    

    【讨论】:

    • 如果我只有一个 ComboBox 会这样做(虽然我正在使用 MVVM 灯) - 但是它是用户定义的 ComboBox 数量,因此 SelectedRep 需要在每个加载的 WeightSet 对象中结束进入 ViewModel
    猜你喜欢
    • 2015-03-18
    • 1970-01-01
    • 2013-11-03
    • 2021-04-16
    • 1970-01-01
    • 2011-03-23
    • 2014-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多