Combobox所选项目未显示On Load

时间:2015-07-16 01:57:30

标签: c# wpf xaml combobox

我的ListView中有ComboBox ItemTemplateComboBox也与使用转换器的ListView绑定在同一列表中。 ComboBox已正确填充,但SelectedItem未显示。

我也试过覆盖基础对象的Equals方法。

XAML:

<ListView x:Name="FactorsListView"
    ItemsSource="{Binding FactorList}" SelectedItem="{Binding SelectedFactor, Mode=OneWayToSource}"
    ScrollViewer.CanContentScroll="False">
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid d:DesignWidth="461.333" d:DesignHeight="368.96">
                <StackPanel>
                    <Grid Height="30.96" Visibility="{Binding IsChecked, ElementName=Monetary, Converter={StaticResource BoolToVis}}">
                        <Label Content="Related Quantitative Factor:" HorizontalAlignment="Left" Margin="10,0,0,0" VerticalAlignment="Top"/>
                        <ComboBox Margin="171.707,4,10,0" VerticalAlignment="Top" Width="Auto" ItemsSource="{Binding DataContext.FactorList, ElementName=UcGrid, Converter={StaticResource QtyFacConverter}}" SelectedItem="{Binding RelatedQuantityFactor}" DisplayMemberPath="Name"/>
                    </Grid>
                </StackPanel>
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
<ListView>

转换器:

public class FactorConverters : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        ObservableCollection<Factor> givenList = value as ObservableCollection<Factor>;
        ObservableCollection<Factor> rList = new ObservableCollection<Factor>();
        if (givenList != null)
        {
            foreach(Factor factor in givenList)
            {
                if (!factor.IsMonetary)
                {
                    rList.Add(factor);
                }
            }
        }
        return rList;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value;
    }
}

因素类:

public class Factor : ModelBase
{
    private Factor _RelatedQuantityFactor;

    public Factor RelatedQuantityFactor
    {
        get
        {
            return _RelatedQuantityFactor;
        }
        set
        {
            if (_RelatedQuantityFactor != value)
            {
                _RelatedQuantityFactor = value;
                RaisePropertyChanged("RelatedQuantityFactor");
            }
        }
    }

    public override bool Equals(object obj)
    {
        if (obj == null || !(obj is Factor))
        {
            return false;
        }
        else
        {
            bool res = ((Factor)obj).ID == this.ID;
            return res;
        }
    }
}

FactorsViewModel类:

public class FactorsViewModel : ViewModelBase
{
    private ObservableCollection<Factor> _FactorList;
    private RevenueItem _SelectedRevenueItem;
    private ObservableCollection<Factor> _UniversalFactors;
    private Factor _SelectedFactor;
    private ObservableCollection<Factor> _QuantitativeFactorHelperList;

    public ObservableCollection<Factor> FactorList
    {
        get
        {
            return _FactorList;
        }
        set
        {
            if (_FactorList != value)
            {
                _FactorList = value;
                AttachFactorListner(value);
            }
        }
    }

    private void AttachFactorListner(ObservableCollection<Factor> value)
    {
        foreach (Factor factor in value)
        {
            factor.PropertyChanged += factor_PropertyChanged;
        }
        RaisePropertyChanged("FactorList");
    }

    void factor_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        if (e.PropertyName == "IsMonetary")
        {
            RaisePropertyChanged("FactorList");
        }
    }


    public RevenueItem SelectedRevenueItem
    {
        get
        {
            return _SelectedRevenueItem;
        }
        set
        {
            if (_SelectedRevenueItem != value)
            {
                _SelectedRevenueItem = value;
                RaisePropertyChanged("SelectedRevenueItem");
            }
        }
    }
    public ObservableCollection<Factor> UniversalFactors
    {
        get
        {
            return _UniversalFactors;
        }
        set
        {
            if (_UniversalFactors != value)
            {
                _UniversalFactors = value;
                RaisePropertyChanged("UniversalFactors");
            }
        }
    }
    public Factor SelectedFactor
    {
        get
        {
            return _SelectedFactor;
        }
        set
        {
            if (_SelectedFactor != value)
            {
                _SelectedFactor = value;
                RaisePropertyChanged("SelectedFactor");
            }
        }
    }
    public ObservableCollection<Factor> QuantitativeFactorHelperList
    {
        get
        {
            return _QuantitativeFactorHelperList;
        }
        set
        {
            if (_QuantitativeFactorHelperList != value)
            {
                _QuantitativeFactorHelperList = value;
                RaisePropertyChanged("QuantitativeFactorHelperList");
            }
        }
    }

    public FactorsViewModel(RevenueItem revenueItem)
    {
        _SelectedRevenueItem = revenueItem;
        _FactorList = revenueItem.Factors;
        AttachFactorListner(_FactorList);

    }


}

视图和视图模型:PostImg Link

3 个答案:

答案 0 :(得分:0)

您应该将SelectedItem绑定到Property

中的某些ViewModel

正如我在您的代码中看到的那样,您将其绑定到模型中的RelatedQuantityFactor Factor,而不是来自ViewModel

答案 1 :(得分:0)

让我们看看 MVVM 模式。 enter image description here

ViewModel 应继承InotifyPropertyChanged,以便更新查看。  模型对象,其中包括属性行为,但它不与查看

因此,如果您想要更新 UI ,则应使用数据绑定。 我认为Factor ViewModel

public class Factor: ModelBase
{
    private ItemViewModel _selectedFactor;
    Public ItemViewModel SelectedFactor
     {
        get
        {
           return _selectedFactor;
        }
        set
        {
         _selectedFactor = value;
          RaisePropertyChanged("RelatedQuantityFactor");
        }
     }
}

您还应该更改绑定模式

SelectedItem="{Binding SelectedFactor, Mode=OneWay}"

最后,不要忘记分配 DataContext

FactorsListView.DataContext = new Factor();

答案 2 :(得分:0)

只是一些问题:

  1. viewmodel是否包含任何变量名称&#34; RelatedQuantityFactor&#34;?
  2. 如果您仍然遇到任何问题,请尝试发布您的代码,其中包括数据上下文和viewmodel的初始化。

    更新: 您可以尝试将断点放入您的setter和GetQuantityFactor的getter中,以调查它是否按预期更新您的相关量子因子

    更新2: 见Difference between SelectedItem, SelectedValue and SelectedValuePath

    希望它的帮助:)