Windows Phone更新/刷新绑定

时间:2014-02-10 13:31:44

标签: c# windows-phone

我的列表框中有一个名为“feedTitle”的文本块,我想更改其forground颜色。我使用Foreground =“{Binding Converter = {StaticResource NewsTextColorConverter}}”来绑定forground颜色。现在奇怪的问题是,如果我在listpicker中选择一种颜色(“Lys”或“Dark”值)它会运行IValueConverter转换方法,但它不显示GUI中的颜色,只有当我重新启动整个应用程序时它显示我选择的颜色。这就像它只设置文本块的forground的颜色一次。

MainPage.xaml中

<ListBox Grid.Row="1" Name="feedListBox" ScrollViewer.VerticalScrollBarVisibility="Auto" SelectionChanged="feedListBox_SelectionChanged">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel VerticalAlignment="Top">
                                <TextBlock TextDecorations="Underline" FontSize="24" Name="feedTitle" TextWrapping="Wrap" Margin="12,0,0,0" Foreground="{Binding Converter={StaticResource NewsTextColorConverter}}" Text="{Binding Title.Text, Converter={StaticResource RssTextTrimmer}}" />
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

在我的app文件中:

的App.xaml

<Application.Resources>
    <converter:NewsTextColorConverter xmlns:converter="clr-namespace:NordjyskeRss" x:Key="NewsTextColorConverter" />
</Application.Resources>

我使用listpicker,用户选择值“Mørk”或“Lys”,然后我希望textblock forground color更新其forground颜色。我调用Convert方法并将null作为参数传递,它似乎运行方法很好:

MainPage.cs

private void lpkThemes_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        // Make sure we don't handle the event during initiation.
        if (e.RemovedItems != null && e.RemovedItems.Count > 0)
        {
            if (this.lpkThemes.SelectedItem != null)
            {
                settings[THEMES_SETTING_KEY] = lpkThemes.SelectedItem.ToString();
                if (lpkThemes.SelectedItem.ToString() == "Mørk")
                {
                    n.Convert(null, null, null, null);
                }
                else
                {
                    n.Convert(null, null, null, null);
                }
            }
        }
    }

这是我使用IValueConverter检查文本块上使用的颜色然后添加它的地方:

MainPage.cs

public class NewsTextColorConverter : IValueConverter
{
    protected IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
    protected const string THEMES_SETTING_KEY = "Themes";

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (settings.Contains(THEMES_SETTING_KEY))
        {
            string themesValue = (string)settings[THEMES_SETTING_KEY];
            if (themesValue == "Mørk")
            {
                return new SolidColorBrush(Colors.Green);
            }
            else
            {
                return new SolidColorBrush(Colors.Blue);
            }
        }
        return new SolidColorBrush(Colors.Green);
        //throw new NotSupportedException("ColorToBurshConverter only supports converting from Color and String");
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

1 个答案:

答案 0 :(得分:0)

我认为您需要按以下方式重新设计您的应用:

  1. 将以下行添加到app.xaml或页面资源中:<SolidColorBrush x:Key="brushListItemsForeground" Color="#FFFFFFFF" />

  2. Foreground="{Binding Converter={StaticResource NewsTextColorConverter}}"替换为Foreground="{StaticResource brushListItemsForeground}"

  3. 在您的SelectionChanged中:

  4. var brush = (SolidColorBrush)Application.Current.Resources["brushListItemsForeground"];如果您已将画笔添加到app.xaml,或= (SolidColorBrush)this.Resources["brushListItemsForeground"];如果您已将画笔添加到页面资源。然后根据您的设置更改画笔的Color属性。

    P.S。还有其他正确的方法:例如创建一个实现INotifyPropertyChanged的SettingsContainer类,将其添加到某个资源字典<local:SettingsContainer x:Key="mySettings" />中,然后绑定到其属性,例如Foreground="{Binding listItemsForeground, Source={StaticResource mySettings}}",当您需要更改值时,请更改类的listItemsForeground属性并引发PropertyChanged。

    目前,您滥用值转换器作为值提供程序,它们不是为此设计的,这就是更新这些值时出现问题的原因。