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

时间:2014-05-28 18:41:08

标签: c# wpf mvvm binding

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

Settings.settings

在我的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>

知道我需要更改什么才能根据设置设置值,并在选择更改时更新应用程序设置?我对C#和WPF很陌生。谢谢!

1 个答案:

答案 0 :(得分:1)

首先见this answer.

您可能不希望Static绑定,它需要某个地方Resources中指定对象的静态实例(例如<Window.Resources>。由于您可能会更改值,我会设置DataContext的{​​{1}}。

RibbonComboBox

在你的代码隐藏编辑:抱歉,这应该在你的模型中,如果适用,必须进行必要的界面更改以公开属性):

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

编辑2 :使用以上编辑的代码。您需要更改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")); } } } 以应用SaveSettings的属性,然后在某处调用它,例如在“保存”this._defaultSettings事件的事件处理程序中。

编辑3 :您需要指定Button.Click作为List<string>位的来源。