2013-03-27 50 views
0

對於我wpfapplication我添加了一個設置文件,其中包含一個顏色的設置:WPF數據綁定組合框<->應用程序設置

MySetting.settings

Name  Type       Value 
myColor System.Windows.Media.Color #FFFFFF 

所以我的自動生成的代碼看起來像:

MySettings.Desinger.cs

[global::System.Configuration.UserScopedSettingAttribute()] 
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] 
    [global::System.Configuration.DefaultSettingValueAttribute("#FFFFFFFF")] 
    public global::System.Windows.Media.Color myColor { 
     get { 
      return ((global::System.Windows.Media.Color)(this["myColor"])); 
     } 
     set { 
      this["myColor"] = value; 
     } 
    } 

這VALU e我想在配置窗口中設置。這是我的XAML代碼:

configWindow.xaml

<Window x:Class="myApp.configWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:properties="clr-namespace:myApp.Properties" 
    xmlns:converter="clr-namespace:myApp.Converter" 
    xmlns:sys="clr-namespace:System;assembly=mscorlib"  
    Title="myApp" Height="557" Width="626"> 
<Window.Resources> 
    <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/> 
    <converter:myColorConverter x:Key="ColorConverter"/> 
    <properties:MySettings x:Key="config"/> 
    <ObjectDataProvider ObjectInstance="{x:Type Colors}" MethodName="GetProperties" x:Key="colorPropertiesOdp" /> 
</Window.Resources> 
<Grid Height="524" Width="615" DataContext="{StaticResource config}"> 
    <TabControl Height="508" HorizontalAlignment="Left" Name="tabControl1" VerticalAlignment="Top" Width="616" BorderThickness="0" Margin="1,11,0,0"> 
     <TabItem Header="options" Name="tabItemOptions"> 
      <Grid Height="484"> 
       <GroupBoxHeight="330" Margin="6,149,15,0" Name="groupBox2" > 
        <Grid Height="313"> 
         <ComboBox x:Name="comboBoxMyColor" Height="23" HorizontalAlignment="Left" Margin="302,34,0,0" VerticalAlignment="Top" Width="120" ItemsSource="{Binding Source={StaticResource colorPropertiesOdp}}" SelectedItem="{Binding Path=Default.myColor, Converter={StaticResource ColorConverter}, Mode=TwoWay}"> 
          <ComboBox.ItemTemplate> 
           <DataTemplate> 
            <StackPanel Orientation="Horizontal"> 
             <Label Background="{Binding Path=Name}" Content="{Binding Path=Name}" Height="{Binding ActualHeight, ElementName=comboBoxNotificationColor}" Width="{Binding ActualWidth, ElementName=comboBoxNotificationColor}"/> 
            </StackPanel> 
           </DataTemplate> 
          </ComboBox.ItemTemplate> 
         </ComboBox> 
        </Grid> 
       </GroupBox> 
      </Grid> 
     </TabItem> 
    </TabControl> 
</Grid> 

但是,如果我試圖改變顏色,我能夠選擇一個值上組合框(如「白」),但配置永遠不會改變。當我在MySettings.Desinger.cs -> myCOlor -> set { ... }上設置斷點時,它從未到達。 我的錯在哪裏?

+0

是'ConvertBack'被稱爲在colorConverter:

關於這個在這裏的信息? – Blachshma 2013-03-27 23:18:17

+0

是的,轉換和convertback被稱爲 – Kooki 2013-03-27 23:24:43

回答

1

在我看來你缺少INotifyPropertyChanged。

如果沒有它,您的模型或視圖模型無法通知您何時從UI中更改某些內容。 WPF-INotifyPropertyChanged

+0

你覺得我需要這個嗎?其他控件(如滑塊或複選框)的原因... – Kooki 2013-03-28 23:09:53