2014-02-10 55 views
-1

我在我的列表框中有一個名爲「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(); 
    } 
} 

回答

0

我認爲你需要重新設計你的應用程序以下列方式:

  1. 添加下面一行到你的App.xaml或頁面資源:<SolidColorBrush x:Key="brushListItemsForeground" Color="#FFFFFFFF" />

  2. 更換Foreground="{Binding Converter={StaticResource NewsTextColorConverter}}"Foreground="{StaticResource brushListItemsForeground}"

  3. 在你的SelectionChanged:

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。

目前,您濫用價值轉換器作爲價值提供者,他們不是爲此設計的,所以您在更新這些值時遇到問題。

+0

謝謝。但我最終使用了一些硬編碼的顏色,在黑暗和光明的主題中都表現出很好的效果。 – Thunder