C# irstOrDefault()。显示名称:SelectedCountry SelectedCountry=\u非洲?countries.Where(=>u.contraction==contraction.Africa).FirstOrDefault()。显示名称:SelectedCountry SelectedCountry=\u America?countries.Where(=>u.Continent==Continent.America)。FirstOrDefault()。显示名称:SelectedCountry

C# irstOrDefault()。显示名称:SelectedCountry SelectedCountry=\u非洲?countries.Where(=>u.contraction==contraction.Africa).FirstOrDefault()。显示名称:SelectedCountry SelectedCountry=\u America?countries.Where(=>u.Continent==Continent.America)。FirstOrDefault()。显示名称:SelectedCountry,c#,wpf,ribbon-control,C#,Wpf,Ribbon Control,调用所有属性的OnPropertyChanged private readonly ObservableCollection<Country> countries; private ContinentViewModel selectedContinent; private static string selectedCountry; private int selectedRadioGroup; private ObservableCollection<ContinentVie

调用所有属性的
OnPropertyChanged

private readonly ObservableCollection<Country> countries;
private ContinentViewModel selectedContinent;
private static string selectedCountry;
private int selectedRadioGroup;
private ObservableCollection<ContinentViewModel> continents;
private ListCollectionView countryView;
public event PropertyChangedEventHandler PropertyChanged;
private bool _All;
private bool _Africa;
private bool _America;
public bool All
{
    get
    {
        return _All;
    }
    set
    {
        _All = value;
        CountryView.Refresh();
        SelectedCountry = _All ? countries.FirstOrDefault().DisplayName : SelectedCountry;
        OnPropertyChanged("All");
    }
}

public bool Africa
{
    get
    {
        return _Africa;
    }
    set
    {
        _Africa = value;
        CountryView.Refresh();
        SelectedCountry = _Africa ? countries.Where(_ => _.Continent == Continent.Africa).FirstOrDefault().DisplayName : SelectedCountry;
        OnPropertyChanged("Africa");
    }
}

public bool America
{
    get
    {
        return _America;
    }
    set
    {
        _America = value;
        CountryView.Refresh();
        SelectedCountry = _America ? countries.Where(_ => _.Continent == Continent.America).FirstOrDefault().DisplayName : SelectedCountry;
        OnPropertyChanged("America");
    }
}

public MySettings()
{
    countries = new ObservableCollection<Country>(
        new[]
        {
        new Country() { Continent = Continent.Africa, DisplayName = "Algeria" },
        new Country() { Continent = Continent.Africa, DisplayName = "Egypt" },
        new Country() { Continent = Continent.Africa, DisplayName = "Chad" },
        new Country() { Continent = Continent.Africa, DisplayName = "Ghana" },
        new Country() { Continent = Continent.America, DisplayName = "Canada" },
        new Country() { Continent = Continent.America, DisplayName = "Greenland" },
        new Country() { Continent = Continent.America, DisplayName = "Haiti" }
        });
    CountryView = (ListCollectionView)CollectionViewSource.GetDefaultView(countries);
    CountryView.Filter += CountryFilter;
    Continents = new ObservableCollection<ContinentViewModel>(Enum.GetValues(typeof(Continent)).Cast<Continent>().Select(c => new ContinentViewModel { Model = c }));
}

private bool CountryFilter(object obj)
{
    var country = obj as Country;
    if (country == null) return false;
    if (All && !Africa && !America) return true;
    else if (!All && Africa && !America) return country.Continent == Continent.Africa;
    else if (!All && !Africa && America) return country.Continent == Continent.America;
    return true;
}

public ObservableCollection<ContinentViewModel> Continents
{
    get { return continents; }
    set
    {
        continents = value;
        OnPropertyChanged("Continents");
    }
}

public ListCollectionView CountryView
{
    get { return countryView; }
    set
    {
        countryView = value;
        OnPropertyChanged("CountryView");
    }
}

public class Country
{
    public string DisplayName { get; set; }
    public Continent Continent { get; set; }
}

public enum Continent
{
    All,
    Africa,
    America
}

public class ContinentViewModel
{
    public Continent Model { get; set; }
    public string DisplayName
    {
        get
        {
            return Enum.GetName(typeof(Continent), Model);
        }
    }
}

public ContinentViewModel SelectedContinent
{
    get { return selectedContinent; }
    set
    {
        selectedContinent = value;
        OnContinentChanged();
        this.OnPropertyChanged("SelectedContinent");
    }
}

private void OnContinentChanged()
{
    CountryView.Refresh();
}

public int SelectedRadioGroup
{
    get { return selectedRadioGroup; }
    set
    {
        selectedRadioGroup = value;
        OnPropertyChanged("SelectedRadioGroup");
    }
}

public string SelectedCountry
{
    get { return selectedCountry; }
    set
    {
        if (selectedCountry == value) return;
        selectedCountry = value;
        OnPropertyChanged("SelectedCountry");
    }
}

protected virtual void OnPropertyChanged(string propertyName)
{
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
私人只读可观察收集国家/地区;
私有大陆视图模型选择大陆;
私有静态字符串selectedCountry;
私有int-selectedRadioGroup;
私人可观测收集大陆;
私有ListCollectionView国家视图;
公共事件属性更改事件处理程序属性更改;
私人住宅;
私人布尔(非洲),;
美国私立学校;
公共图书馆
{
得到
{
全部归还;
}
设置
{
_全部=价值;
CountryView.Refresh();
SelectedCountry=\u All?countries.FirstOrDefault()。显示名称:SelectedCountry;
不动产变更(“全部”);
}
}
公共布尔非洲
{
得到
{
返回非洲;
}
设置
{
_非洲=价值;
CountryView.Refresh();
SelectedCountry=\u Africa?countries.Where(\u=>\u.Continental==Continental.Africa)。FirstOrDefault()。显示名称:SelectedCountry;
不动产变更(“非洲”);
}
}
美国公共图书馆
{
得到
{
回归美国;
}
设置
{
_美国=价值;
CountryView.Refresh();
SelectedCountry=\u America?countries.Where(\u=>\u.Continental==Continental.America)。FirstOrDefault()。显示名称:SelectedCountry;
房地产变更(“美国”);
}
}
公共MySettings()
{
国家/地区=新的可观测集合(
新[]
{
新国家(){大陆=大陆.非洲,DisplayName=“阿尔及利亚”},
新国家(){大陆=大陆.非洲,DisplayName=“埃及”},
新国家(){大陆=大陆.非洲,DisplayName=“乍得”},
新国家(){大陆=大陆.非洲,DisplayName=“加纳”},
new Country(){大陆=大陆.美洲,DisplayName=“加拿大”},
new Country(){大陆=大陆.美洲,DisplayName=“格陵兰”},
新国家({大陆=大陆.美洲,DisplayName=“海地”}
});
CountryView=(ListCollectionView)CollectionViewSource.GetDefaultView(国家);
CountryView.Filter+=CountryFilter;
大陆=新的ObservableCollection(Enum.GetValues(typeof(大陆)).Cast().Select(c=>new Continents ViewModel{Model=c}));
}
私有布尔CountryFilter(对象对象对象)
{
var国家=obj作为国家;
如果(country==null)返回false;
如果(所有&&!非洲&&!美洲)返回true;
否则,如果(!All&&Africa&&!America)返回country.containment==containment.Africa;
如果(!All&&!Africa&&America)返回country.Continent==Continent.America;
返回true;
}
公共观测收集大陆
{
获取{返回大陆;}
设置
{
大陆=价值;
财产变更(“大陆”);
}
}
公共列表集合视图CountryView
{
获取{return countryView;}
设置
{
countryView=价值;
OnPropertyChanged(“CountryView”);
}
}
公营国家
{
公共字符串DisplayName{get;set;}
公共大陆{get;set;}
}
欧洲大陆公共普查
{
全部的
非洲,
美国
}
公共类模型
{
公共大陆模型{get;set;}
公共字符串显示名
{
得到
{
返回Enum.GetName(typeof(大陆),Model);
}
}
}
公共大陆视图模型选定大陆
{
获取{返回SelectedContinental;}
设置
{
SelectedContinental=值;
OnContinentChanged();
此。OnProperty已更改(“选定大陆”);
}
}
私有void OnContinentChanged()
{
CountryView.Refresh();
}
public int SelectedRadioGroup
{
获取{return selectedRadioGroup;}
设置
{
selectedRadioGroup=值;
OnPropertyChanged(“SelectedRadioGroup”);
}
}
公共字符串SelectedCountry
{
获取{返回selectedCountry;}
设置
{
如果(selectedCountry==值)返回;
selectedCountry=值;
已更改的房地产(“选定国家”);
}
}
受保护的虚拟void OnPropertyChanged(字符串propertyName)
{
PropertyChanged?.Invoke(这是新的PropertyChangedEventArgs(propertyName));
}

尝试将
ListCollectionView
创建为
CountryView=CollectionViewSource.GetDefaultView(国家)我必须将其转换为
CountryView=(ListCollectionView)CollectionViewSource.GetDefaultView(国家)并且它确实防止了主错误。但是,我现在得到错误
System.Windows.Data错误:4:找不到引用为“RelativeSource FindAncestor,AncestorType='System.Windows.Controls.ItemsControl',AncestorLevel='1'的绑定源。BindingExpression:Path=HorizontalContentAlignment;DataItem=null;目标元素是“RibbonGalleryItem”(名称=“”);目标属性为“HorizontalContentAlignment”(类型为“HorizontalAlignment”)
。这与我使用组合框时收到的消息相同。此外,当我单击单选按钮时,它会显示
MyNamespace.MySettings+Country
,而不是第一个选项,尽管国家确实出现在下拉菜单中。您已将SelectedCountry绑定到组合框的SelectedItem,无法在code Behind中查看属性。关于您收到的另一个错误,我看不到任何代码,请作为单独的问题提问在我的原始代码中。我现在已经将其包含在上面的代码中。谢谢您的帮助。尽管组合框仍然显示
namespace.MySettings+Country
,但仍然存在问题。我将您的代码复制到我的项目中,并删除了所有其他代码。如果运行代码,请选择一个国家(如Alg
<Grid>
    <DockPanel>
        <r:Ribbon DockPanel.Dock="Top" x:Name="Ribbon">
            <r:RibbonGroup Header="Continent" Width="260">
                <!--<ComboBox x:Name="CountryList" Width="100" ItemsSource="{Binding CountryView}" SelectedItem="{Binding SelectedCountry}" DisplayMemberPath="DisplayName"/>-->
                <r:RibbonComboBox x:Name="CountryList" Height="Auto" SelectionBoxWidth="230" VerticalAlignment="Center">
                    <r:RibbonGallery x:Name="cbSelectedCountry" SelectedValue="{Binding SelectedCountry, Mode=TwoWay}" SelectedValuePath="DisplayName" >
                        <r:RibbonGalleryCategory x:Name="cbCountryList" ItemsSource="{Binding CountryView}" DisplayMemberPath="DisplayName" />
                    </r:RibbonGallery>
                </r:RibbonComboBox>
                <WrapPanel>
                    <r:RibbonRadioButton x:Name="All" Label="All" GroupName="ContinentGroup" Height="Auto" Width="Auto" HorizontalAlignment="Left" IsChecked="{Binding Path=All}">
                    </r:RibbonRadioButton>
                    <r:RibbonRadioButton x:Name="Africa" Label="Africa" GroupName="ContinentGroup" Height="Auto" Width="Auto" HorizontalAlignment="Left" IsChecked="{Binding Path=Africa}">
                    </r:RibbonRadioButton>
                    <r:RibbonRadioButton x:Name="America" Label="America" GroupName="ContinentGroup" Height="Auto" Width="Auto" HorizontalAlignment="Left" IsChecked="{Binding Path=America}">
                    </r:RibbonRadioButton>
                </WrapPanel>
            </r:RibbonGroup>
        </r:Ribbon>
    </DockPanel>
</Grid>
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows.Data;

public class MySettings : INotifyPropertyChanged
{
    private readonly ObservableCollection<Country> countries;
    private ContinentViewModel selectedContinent;
    private static string selectedCountry;
    private int selectedRadioGroup;
    private ObservableCollection<ContinentViewModel> continents;
    private ListCollectionView countryView;
    public event PropertyChangedEventHandler PropertyChanged;
    private bool _All;
    private bool _Africa;
    private bool _America;

    public bool All { get => _All; set { _All = value; CountryView.Refresh(); SelectedCountry = countries[0].ToString(); } }
    public bool Africa { get => _Africa; set { _Africa = value; CountryView.Refresh(); SelectedCountry = countries[0].ToString(); } }
    public bool America { get => _America; set { _America = value; CountryView.Refresh(); SelectedCountry = countries[0].ToString(); } }

    public MySettings()
    {
        countries = new ObservableCollection<Country>(
            new[]
            {
                new Country() { Continent = Continent.Africa, DisplayName = "Algeria" },
                new Country() { Continent = Continent.Africa, DisplayName = "Egypt" },
                new Country() { Continent = Continent.Africa, DisplayName = "Chad" },
                new Country() { Continent = Continent.Africa, DisplayName = "Ghana" },
                new Country() { Continent = Continent.America, DisplayName = "Canada" },
                new Country() { Continent = Continent.America, DisplayName = "Greenland" },
                new Country() { Continent = Continent.America, DisplayName = "Haiti" }
            });
        CountryView = (ListCollectionView)CollectionViewSource.GetDefaultView(countries);
        CountryView.Filter += CountryFilter;
        Continents = new ObservableCollection<ContinentViewModel>(Enum.GetValues(typeof(Continent)).Cast<Continent>().Select(c => new ContinentViewModel { Model = c }));
    }

    private bool CountryFilter(object obj)
    {
        var country = obj as Country;
        if (country == null) return false;
        if (All) return true;
        if (Africa) return country.Continent == Continent.Africa;
        if (America) return country.Continent == Continent.America;
        return true;
    }

    public ObservableCollection<ContinentViewModel> Continents
    {
        get { return continents; }
        set
        {
            continents = value;
        }
    }

    public ListCollectionView CountryView
    {
        get { return countryView; }
        set
        {
            countryView = value;
        }
    }

    public class Country
    {
        public string DisplayName { get; set; }
        public Continent Continent { get; set; }
    }

    public enum Continent
    {
        All,
        Africa,
        America
    }

    public class ContinentViewModel
    {
        public Continent Model { get; set; }
        public string DisplayName
        {
            get
            {
                return Enum.GetName(typeof(Continent), Model);
            }
        }
    }

    public ContinentViewModel SelectedContinent
    {
        get { return selectedContinent; }
        set
        {
            selectedContinent = value;
            OnContinentChanged();
            this.OnPropertyChanged("SelectedContinent");
        }
    }

    private void OnContinentChanged()
    {
        CountryView.Refresh();
    }

    public int SelectedRadioGroup
    {
        get { return selectedRadioGroup; }
        set
        {
            selectedRadioGroup = value;
        }
    }

    public string SelectedCountry
    {
        get { return selectedCountry; }
        set
        {
            if (selectedCountry == value) return;
            selectedCountry = value;
        }
    }

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}
private readonly ObservableCollection<Country> countries;
private ContinentViewModel selectedContinent;
private static string selectedCountry;
private int selectedRadioGroup;
private ObservableCollection<ContinentViewModel> continents;
private ListCollectionView countryView;
public event PropertyChangedEventHandler PropertyChanged;
private bool _All;
private bool _Africa;
private bool _America;
public bool All
{
    get
    {
        return _All;
    }
    set
    {
        _All = value;
        CountryView.Refresh();
        SelectedCountry = _All ? countries.FirstOrDefault().DisplayName : SelectedCountry;
        OnPropertyChanged("All");
    }
}

public bool Africa
{
    get
    {
        return _Africa;
    }
    set
    {
        _Africa = value;
        CountryView.Refresh();
        SelectedCountry = _Africa ? countries.Where(_ => _.Continent == Continent.Africa).FirstOrDefault().DisplayName : SelectedCountry;
        OnPropertyChanged("Africa");
    }
}

public bool America
{
    get
    {
        return _America;
    }
    set
    {
        _America = value;
        CountryView.Refresh();
        SelectedCountry = _America ? countries.Where(_ => _.Continent == Continent.America).FirstOrDefault().DisplayName : SelectedCountry;
        OnPropertyChanged("America");
    }
}

public MySettings()
{
    countries = new ObservableCollection<Country>(
        new[]
        {
        new Country() { Continent = Continent.Africa, DisplayName = "Algeria" },
        new Country() { Continent = Continent.Africa, DisplayName = "Egypt" },
        new Country() { Continent = Continent.Africa, DisplayName = "Chad" },
        new Country() { Continent = Continent.Africa, DisplayName = "Ghana" },
        new Country() { Continent = Continent.America, DisplayName = "Canada" },
        new Country() { Continent = Continent.America, DisplayName = "Greenland" },
        new Country() { Continent = Continent.America, DisplayName = "Haiti" }
        });
    CountryView = (ListCollectionView)CollectionViewSource.GetDefaultView(countries);
    CountryView.Filter += CountryFilter;
    Continents = new ObservableCollection<ContinentViewModel>(Enum.GetValues(typeof(Continent)).Cast<Continent>().Select(c => new ContinentViewModel { Model = c }));
}

private bool CountryFilter(object obj)
{
    var country = obj as Country;
    if (country == null) return false;
    if (All && !Africa && !America) return true;
    else if (!All && Africa && !America) return country.Continent == Continent.Africa;
    else if (!All && !Africa && America) return country.Continent == Continent.America;
    return true;
}

public ObservableCollection<ContinentViewModel> Continents
{
    get { return continents; }
    set
    {
        continents = value;
        OnPropertyChanged("Continents");
    }
}

public ListCollectionView CountryView
{
    get { return countryView; }
    set
    {
        countryView = value;
        OnPropertyChanged("CountryView");
    }
}

public class Country
{
    public string DisplayName { get; set; }
    public Continent Continent { get; set; }
}

public enum Continent
{
    All,
    Africa,
    America
}

public class ContinentViewModel
{
    public Continent Model { get; set; }
    public string DisplayName
    {
        get
        {
            return Enum.GetName(typeof(Continent), Model);
        }
    }
}

public ContinentViewModel SelectedContinent
{
    get { return selectedContinent; }
    set
    {
        selectedContinent = value;
        OnContinentChanged();
        this.OnPropertyChanged("SelectedContinent");
    }
}

private void OnContinentChanged()
{
    CountryView.Refresh();
}

public int SelectedRadioGroup
{
    get { return selectedRadioGroup; }
    set
    {
        selectedRadioGroup = value;
        OnPropertyChanged("SelectedRadioGroup");
    }
}

public string SelectedCountry
{
    get { return selectedCountry; }
    set
    {
        if (selectedCountry == value) return;
        selectedCountry = value;
        OnPropertyChanged("SelectedCountry");
    }
}

protected virtual void OnPropertyChanged(string propertyName)
{
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}