2017-04-18 3 views
1

이전에 몇 가지 대답을 확인했지만 도움이되지 않았습니다. StackLayout BackgroundColor를 바인딩하여 ListView에서 ViewCell의 배경색을 변경하려고합니다. 지금은 다음과 같습니다. enter image description hereXamarin에서 바인딩 색 목록보기

모든 셀은 다른 색으로 채워야하지만 전혀 변경되지 않습니다. 그 뒤에 코드 :

OrderDatapage.XAML :

<ContentPage.Resources> 
    <ResourceDictionary> 
     <local:BackgroundConverter x:Key="BackgroundConverter" /> 
    </ResourceDictionary> 
</ContentPage.Resources> 
. 
. 
. 
. 
<StackLayout Orientation="Vertical"> 
     <ListView x:Name="timetableList" 
         RowHeight="25" 
         SeparatorVisibility="Default" 
         Margin="0,0,0,10" 
         > 
       <ListView.ItemTemplate> 
        <DataTemplate> 
         <ViewCell> 
          <StackLayout Orientation="Horizontal" 
             VerticalOptions="FillAndExpand" 
             BackgroundColor="{Binding Paint, Converter={StaticResource BackgroundConverter}}"> 
             <Label Text="{Binding Number}" 
               FontSize="Medium" 
               Margin="20,0,0,0" 
               TextColor="White" 
               BackgroundColor="Black" 
               /> 
             <Label Text="{Binding Title}" 
               FontSize="Default" 
               Margin="20,0,0,0" 
               TextColor="Black" 
               /> 
             <Label Text="{Binding Date}" 
               FontSize="Default" 
               HorizontalOptions="EndAndExpand" 
               Margin="0,0,40,0" 
               TextColor="Black" 
               /> 
          </StackLayout> 
         </ViewCell> 
        </DataTemplate> 
       </ListView.ItemTemplate> 
     </ListView> 
    </StackLayout> 

Converter.cs

class BackgroundConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     return Color.FromHex(value.ToString()); 
    } 
} 

OrderDataPage.cs

List<TimetableItem> timeTableList { get; set; } 
    public OrderDataPage() 
    { 
     InitializeComponent(); 
     timeTableList = new List<TimetableItem>(); 
     var timetable1 = new TimetableItem() { Number = "1", Title = "Test1", Date = "20-12-2020", Paint = "#63FF20" }; 
     var timetable2 = new TimetableItem() { Number = "2", Title = "Test2", Date = "20-12-2020", Paint = "#FFD933" }; 
     var timetable3 = new TimetableItem() { Number = "3", Title = "Test3", Date = "20-12-2020", Paint = "#C0C0C0" }; 
     timeTableList.Add(timetable1); 
     timeTableList.Add(timetable2); 
     timeTableList.Add(timetable3); 

     timetableList.ItemsSource = timeTableList; 

    } 
+0

을 찾을 수 있습니다 강조한다는 것입니다 IValueConverter 없이도 작동 귀하 코드가 잘 보입니다. 중단 점을 확인 했습니까? 값 변환이 호출되는 중 하나입니까? – Dilmah

답변

3

난 당신이 라벨의 backgroundColor로를 "시도해야한다고 생각 투명 "StackLayout 배경색을 볼 수 있지만 작동하는지 확실하지 않습니다. 그렇지 않으면 Label의 BackGroundColor를 특정 색상으로 설정할 수 있습니다. "값이"null가 아닌 경우 IValueConverter에서

당신의 backgroundColor가 어디

if(value != null && value is string && (string)value != "") 
    return Color.FromHex(value.ToString()); 
else 
    return Color.Red; 

가 UPDATE

내가 StackLayout의 BackgroudColor

slView.SetBinding(StackLayout.BackgroundColorProperty, "BackgroundColor"); 

설정에 약간의 테스트를 수행 한 확인해야합니다 문자열

List.Add(new Model { Description = "D1", Cost = 10.0, Qty = 1, BackgroundColor = "#9ac16e"}); 
List.Add(new Model { Description = "D2", Cost = 20.0, Qty = 2, BackgroundColor = "#8d0000" }); 
List.Add(new Model { Description = "D3", Cost = 30.0, Qty = 3, BackgroundColor = "#3a6cf6"}); 

그리고

iOS

Android

유일한 문제는 당신이 행

를 선택할 때 느슨한 다음 당신은 REPO HERE

+0

레이블의 BackGroundColor를 "투명"으로 설정하는 것이 좋습니다. 고맙습니다! – user2999425