2015-04-02 89 views
0

在重複標記之前,請仔細閱讀我的問題。在DataTemplate中訪問填充矩形的顏色

我正在製作xaml c#VS2013中的Windows Phone應用程序。 我正在使用longlistselector中的Web API(因爲我想讓用戶可以像列表框中一樣選擇項目)。

XAML

    <phone:LongListSelector Margin="24,0" x:Name="Longlist" d:IsHidden="True" ItemTemplate="{StaticResource GoalTemplate}"/> 



<phone:PhoneApplicationPage.Resources> 
    <DataTemplate x:Key="GoalTemplate"> 
     <Grid Width="432" Height="90" Margin="0,33,0,0"> 
      <StackPanel Orientation="Horizontal"> 
       <Rectangle HorizontalAlignment="Left" Height="90" StrokeThickness="0" VerticalAlignment="Top" Width="4"> 
        <Rectangle.Fill> 
         <SolidColorBrush Color="{Binding rcolor, Mode=OneWay}"/> 
        </Rectangle.Fill> 
       </Rectangle> 
       <StackPanel HorizontalAlignment="Left" Height="86" Margin="27,4,0,0" VerticalAlignment="Top" Width="400"> 
        <TextBlock Text="{Binding Name}" HorizontalAlignment="Left" Height="37" TextWrapping="Wrap" VerticalAlignment="Top" Width="405" Foreground="{StaticResource FlatUI-Blue1}" FontSize="26.667"/> 
        <TextBlock Text="{Binding Description}" TextWrapping="Wrap" FontSize="14.667" Height="49" Foreground="{StaticResource FlatUI-Grey}"/> 
       </StackPanel> 
      </StackPanel> 
     </Grid> 
    </DataTemplate> 

</phone:PhoneApplicationPage.Resources> 

XAML.CS代碼

void Maths_Loaded(object sender, RoutedEventArgs e) 
    { 
     string uri = "http://localhost:1361/api/chore"; 
     WebClient client = new WebClient(); 
     client.Headers["Accept"] = "application/json"; 
     client.DownloadStringAsync(new Uri(uri)); 
     client.DownloadStringCompleted += (s1, e1) => 
     { 
      //var data = JsonConvert.DeserializeObject<HomeWork[]>(e1.Result.ToString()); 
      //MessageBox.Show(data.ToString()); 
      var hw = JsonConvert.DeserializeObject<HomeWork[]>(e1.Result.ToString()); 
      foreach (HomeWork c in hw) 
      { 
       if (c.Chore_Type == "Maths") 
       { 
        result.Add(c); 
        // result is a List<HomeWork> to store only maths 
        // homework in longlistselector 
       } 

      } 

      Longlist.ItemsSource = result; 


     }; 
    } 

現在我的問題是 我有一個長方形指示作業的優先級。 在數據庫中,我擁有包含「正常」,「中」和「高」優先級的優先級列。 我想讓Rectangle的填充顏色分別爲藍色,橙色和綠色。 但是我無法改變矩形的顏色(從Web API消耗)

請指導我如何訪問數據模板中的矩形應用if-else子句或其他改變基於優先級的顏色。

謝謝 請指導我。 如果你DONOT明白,請告知這樣我就可以解釋的。

回答

1

我的功課類明白你有一個叫類型的優先級屬性枚舉或字符串,

執行以下操作:

1:綁定矩形填充顏色,以該財產與轉換器:

<Rectangle HorizontalAlignment="Left" Height="90" StrokeThickness="0" VerticalAlignment="Top" Width="4"> 
     <Rectangle.Fill> 
       <SolidColorBrush Color="{Binding Priority, Converter={StaticResource PriorityColorConverter}"/> 
     </Rectangle.Fill> 
    </Rectangle> 

2:現在通過以下方式添加一個轉換器:

<Page...> 
... 
<Page.Resources> 
<Converters:PriorityColorConverter x:Key="PriorityColorConverter"/> 
</Page.Resources> 

3 :並創建該轉換器:

public class PriorityColorConverter : IValueConverter 
    { 
    public object Convert(object value, Type targetType, object parameter, string language) 
    { 
     var priority = (Priorities)value; 

     if (priority == Priorities.High) 
      return Colors.Blue; 
     else if (priority == Priorities.Medium) 
      return Colors.Orange; 
     else 
      return Colors.Green; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, string language) 
    { 
     throw new NotImplementedException(); 
    } 
} 

當然,在頁面中添加xmlns命名空間創造了轉換器。

+0

謝謝。這種方法幫助我解決了這個問題。 – 2015-04-03 05:47:29