0

对于我的 wpfapplication,我添加了一个设置文件,其中包含一个颜色作为设置:

我的设置.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;
        }
    }

我想在配置窗口上设置这个值。这是我的 xaml 代码:

配置窗口.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 { ... },它永远不会到达。我的错在哪里?

4

1 回答 1

1

在我看来,您缺少 INotifyPropertyChanged 。

没有它,当您从 ui 更改某些内容时,您的模型或视图模型无法通知。

此处的相关信息:WPF-INotifyPropertyChanged

于 2013-03-27T23:55:23.627 回答