1

アプリケーション プロパティを次のようにマッピングします。

<Application.Resources>
    <properties:Settings x:Key="Settings" />
</Application.Resources>

目標は、フォント サイズ設定MainWindowFontSize (int) をコンボボックスで選択した値にバインドすることです。

<ComboBox 
  SelectedValuePath="Content"
  SelectedValue="{Binding Default.MainWindowFontSize, Source={StaticResource Settings}}">
<ComboBoxItem>8</ComboBoxItem>
...
<ComboBoxItem>48</ComboBoxItem>
</ComboBox>

これに関する問題は、設定から ComboBox への一方向でのみ機能することですが、コンボの選択は設定に戻らないということです。モデルのフォント サイズに通常のプロパティを使用すると、すべて正常に動作するように見えます...

バインディングを双方向の設定で動作させる方法について何か提案はありますか?

4

3 に答える 3

2

.NET 4.5 の新しい機能のようです。ただし、コード ビハインドでバインディングを作成すると、問題なく動作することがわかりました。そのようです:

    public MainWindow()
    {
        InitializeComponent();
        var binding = new Binding("Delay");
        binding.Source = Settings.Default;
        binding.Mode = BindingMode.TwoWay;
        BindingOperations.SetBinding(this.Combo, ComboBox.SelectedValueProperty, binding);
    }
于 2012-12-01T05:50:03.190 に答える
1

バインディングのモードを TwoWay に設定してみましたか?

<ComboBox 
  SelectedValuePath="Content"
  SelectedValue="{Binding Default.MainWindowFontSize, Source={StaticResource Settings}, Mode=TwoWay}">

UpdateSourceTrigger も試すことができます。

 <ComboBox 
  SelectedValuePath="Content"
  SelectedValue="{Binding Default.MainWindowFontSize, Source={StaticResource Settings}, Mode=TwoWay}, UpdateSourceTrigger=PropertyChanged">
于 2012-11-29T19:13:38.687 に答える
1

この回避策を見つけました:

<ComboBox ...  SelectionChanged="MainWndFontSizeSelectionChanged" ...>

イベント ハンドラ:

private void MainWndFontSizeSelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var cb = (ComboBox)sender;
    int newSize = 0;
    if (Int32.TryParse(cb.SelectedValue.ToString(), out newSize) == true)
    {
        WpfApplication1.Properties.Settings.Default.MainWindowFontSize = newSize;
    }
}

醜いですが、うまくいきます...より良い解決策が現れることを願っています...

この投稿では、表示される問題についてより多くの洞察を提供しています: LINK

.NET4.5 では、以前のバージョンと同じようには機能しません。

于 2012-11-29T20:35:35.313 に答える