Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将RibbonComboBox选择绑定到应用程序设置?_C#_Wpf_Mvvm_Binding - Fatal编程技术网

C# 如何将RibbonComboBox选择绑定到应用程序设置?

C# 如何将RibbonComboBox选择绑定到应用程序设置?,c#,wpf,mvvm,binding,C#,Wpf,Mvvm,Binding,我有一个RibbonComboBox,希望根据我的应用程序设置设置combobox选项,并在combobox选项更改时存储对应用程序设置的任何更改。我尝试使其工作,但初始选择不起作用,并且更改选择似乎不会更新设置值(当应用程序退出时,我执行Properties.Settings.Default.Save()操作) 在我的xaml中,我有: xmlns:p="clr-namespace:Scanning.Properties" <RibbonComboBox IsEditable="

我有一个RibbonComboBox,希望根据我的应用程序设置设置combobox选项,并在combobox选项更改时存储对应用程序设置的任何更改。我尝试使其工作,但初始选择不起作用,并且更改选择似乎不会更新设置值(当应用程序退出时,我执行Properties.Settings.Default.Save()操作)

在我的xaml中,我有:

 xmlns:p="clr-namespace:Scanning.Properties"

 <RibbonComboBox IsEditable="False">
     <RibbonGallery SelectedItem="{Binding Source={x:Static p:Settings.Default}, Path=profile1_papersize, Mode=TwoWay}">
         <RibbonGalleryCategory>
             <RibbonGalleryItem Content="A4" />
             <RibbonGalleryItem Content="B5" />
             <RibbonGalleryItem Content="Letter" />
             <RibbonGalleryItem Content="Legal" />
         </RibbonGalleryCategory>
     </RibbonGallery>
 </RibbonComboBox>
xmlns:p=“clr命名空间:Scanning.Properties”
你知道我需要做什么更改才能使它根据设置设置值,并在选择更改时更新应用程序设置吗?我对C#和WPF很陌生。谢谢

先看

您可能不希望使用
静态
绑定,该绑定需要
资源
中某个位置(例如
)中指定对象的静态实例。由于您可能正在更改该值,因此我将改为设置
RibbonComboBox
数据上下文

<RibbonComboBox IsEditable="False" DataContext="{Binding Path=DefaultSettings}">
    <RibbonGallery SelectedItem="{Binding Path=profile1_papersize, Mode=TwoWay}">
        <RibbonGalleryCategory ItemsSource="{Binding Path=PaperSizes}">
        </RibbonGalleryCategory>
    </RibbonGallery>
</RibbonComboBox>

在codebehind中(编辑:抱歉,这应该在您的模型中,并进行必要的界面更改以公开属性(如果适用):

publicYourModel:INotifyPropertyChanged
{
公共模型()
{
//构造函数代码。。。
}
#区域INotifyProperty更改成员
公共事件属性更改事件处理程序属性更改;
#endregion InotifyProperty更改成员
公共void SaveSettings()
{
//用于复制此的代码。\u defaultSettings到Scanning.Properties.Settings.Default
Scanning.Properties.Settings.Default.profile1\u papersize=此。\u defaultSettings.profile1\u papersize;
Scanning.Properties.Settings.Default.OtherProperty1=这个;
Scanning.Properties.Settings.Default.OtherProperty2=这个;
Scanning.Properties.Settings.Default.OtherProperty3=这个;
//保存Scanning.Properties.Settings.Default的代码
}
私有静态只读列表defaultPaperSizes=新列表
{
“A4”,
“B5”,
“信”,
“合法”,
};
私人名单(纸张尺寸);;
公开名单纸张尺寸
{
得到
{
如果(this.\u papersize=null)this.papersize=defaultpapersize;
返回此文件。\u纸张尺寸;
}
设置
{
该值为.\u papersize=值;
if(this.PropertyChanged!=null)
此.PropertyChanged(此,新PropertyChangedEventArgs(“纸张尺寸”);
}
}
private YourSettingsType\u defaultSettings;
公共设置键入默认设置
{ 
得到
{ 
如果(this._defaultSettings==null)this.defaultSettings=Scanning.Properties.Settings.Default;
返回此项。\u defaultSettings;
}
设置
{ 
这是。_defaultSettings=值;
if(this.PropertyChanged!=null)
this.PropertyChanged(这是新的propertychangedventargs(“DefaultSettings”);
}
}
}
编辑2:使用上面编辑的代码。您需要更改
保存设置
以应用
此的属性。\u defaultSettings
,然后在某个地方调用它,例如在事件处理程序中调用“保存”
按钮。单击事件或其他内容


编辑3:您需要指定一个
列表
作为
RibbonGalleryCategory
位的源。

这是正确的答案-只需将INotifyPropertyChanged的一个实现添加到此位置,您就可以通过通知双向绑定。@TroelsLarsen哦,是的,忘记了
INotifyPropertyChanged
。我会的还可以编辑以演示如何使用设置。@klugerama,这是一个很好的示例,我想我已经差不多做到了。我给了你奖励点。我有点绊倒,因为我遗漏了YourModel()构造函数。我认为需要一个空构造函数。我想我遇到的最后一个问题是,设置值被存储为字符串“System.Windows.Controls.Ribbon.RibbonGalleryItem:字母”而不是“字母”"。有没有一个简单的解决方法?@SeanN。可能在XAML中使用
SelectedValue
而不是
SelectedItem
。@klugerama,改为SelectedValue,其行为相同。不知怎的,我需要将RibbonGalleryItem.Content放入_defaultSettings.profile1\u papersize而不是RibbonGalleryItem。我不确定如何做到这一点嘘,谢谢。
public YourModel : INotifyPropertyChanged
{
    public YourModel()
    {
        // Contructor code...
    }

    #region INotifyPropertyChanged Members
    public event PropertyChangedEventHandler PropertyChanged;
    #endregion INotifyPropertyChanged Members

    public void SaveSettings()
    {
        // code to copy this._defaultSettings to Scanning.Properties.Settings.Default
        Scanning.Properties.Settings.Default.profile1_papersize = this._defaultSettings.profile1_papersize;
        Scanning.Properties.Settings.Default.OtherProperty1 = this._defaultSettings.Property1;
        Scanning.Properties.Settings.Default.OtherProperty2 = this._defaultSettings.Property2;
        Scanning.Properties.Settings.Default.OtherProperty3 = this._defaultSettings.Property3;
        // code to save Scanning.Properties.Settings.Default
    }

    private static readonly List<string> defaultPaperSizes = new List<string> 
                                                             {
                                                                 "A4",
                                                                 "B5",
                                                                 "Letter",
                                                                 "Legal",
                                                             };
    private List<string> _paperSizes;
    public List<string> PaperSizes
    {
        get
        {
            if (this._paperSizes = null) this.PaperSizes = defaultPaperSizes;
            return this._paperSizes;
        }
        set
        {
            this._paperSizes = value;
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs("PaperSizes"));
        }
    }

    private YourSettingsType _defaultSettings;
    public YourSettingsType DefaultSettings
    { 
        get 
        { 
            if (this._defaultSettings == null) this.DefaultSettings = Scanning.Properties.Settings.Default; 
            return this._defaultSettings;
        }
        set 
        { 
            this._defaultSettings = value;
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs("DefaultSettings"));
        }
    }
}