我在我的列表框中有一個名爲「feedTitle」的文本塊,我想要更改它的forground顏色。我使用Foreground =「{Binding Converter = {StaticResource NewsTextColorConverter}}」來綁定forground的顏色。現在奇怪的問題是,如果我在listpicker(「Lys」或「Dark」值)中選擇一種顏色,它將運行IValueConverter Convert方法,但它不顯示GUI中的顏色,只有當我重新啓動整個應用程序時顯示我選擇的顏色。這就像它只設置了文本塊的forground的顏色一次。Windows Phone更新/刷新綁定
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.xaml
<Application.Resources>
<converter:NewsTextColorConverter xmlns:converter="clr-namespace:NordjyskeRss" x:Key="NewsTextColorConverter" />
</Application.Resources>
我使用listpicker,其中用戶選擇值 「莫克」 或「賴氨酸「然後我想要textblock forground顏色更新它的forground顏色。我所說的轉換方法,並傳遞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();
}
}
謝謝。但我最終使用了一些硬編碼的顏色,在黑暗和光明的主題中都表現出很好的效果。 – Thunder