2017-12-02 4 views
2

바인드 된 속성의 바인딩을 사용하여 UWP 응용 프로그램에서 listitem의 배경색을 설정하거나 테스트 목적으로 TextBlock의 텍스트 전경색을 설정하려고합니다 항목 (TimeEntry).UWP 응용 프로그램에서 바인딩 및 변환기로 배경색을 설정하지 않고 있습니다.

이 TimeEntries의 모음 (초 마지막 줄에 해당하는 TextBlock)에 바인딩 된 ListView에 대한 XAML입니다 :

... 
    <Page.Resources> 
     <local:TimeEntryTypeColorConverter x:Key="TimeEntryTypeColorConverter" /> 
    </Page.Resources> 
... 
<StackPanel Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="1"> 
      <TextBlock Text="Xy:" /> 
      <ListView> 
       <ItemsControl.ItemsPanel> 
        <ItemsPanelTemplate> 
         <StackPanel></StackPanel> 
        </ItemsPanelTemplate> 
       </ItemsControl.ItemsPanel> 
       <ItemsControl.ItemTemplate> 
        <DataTemplate> 
         <Grid> 
          <Grid.RowDefinitions> 
           <RowDefinition Height="Auto"></RowDefinition> 
          </Grid.RowDefinitions> 
          <Grid.ColumnDefinitions> 
           <ColumnDefinition Width="Auto"></ColumnDefinition> 
           .... 
          </Grid.ColumnDefinitions> 
          <TextBlock Text="Test" Grid.Row="0" Grid.Column="0" Foreground="{Binding Type, Converter={StaticResource TimeEntryTypeColorConverter}}" /> 
          ... 

TimeEntry 클래스는 'TimeEntryType'과 '유형'의 열거가 있습니다 특성 :

public enum TimeEntryType 
    { 
     Unknown, 
     Standard, 
     Break 
    } 

public TimeEntryType Type 
{ 
    get 
    { 
     if (_isBreak) 
     { 
      return TimeEntryType.Break; 
     } 
     return TimeEntryType.Standard; 
    } 
} 

그리고 이것은이 속성/열거의 컨버터 모습입니다 같은 :

public class TimeEntryTypeColorConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, string language) 
    { 
     if (value == null) 
      return null; 
     var timeEntryType = (TimeEntry.TimeEntryType)value; 
     if (timeEntryType == TimeEntry.TimeEntryType.Break) 
      return Colors.LightGray; 

     return Colors.Transparent; 
    } 

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

다른 ListTime 항목 컬렉션의 TimeEntry 개체에 대한 바인딩이 있습니다. 그리고 디버거가 변환기가 사용 중이며 'Break'에 대한 예를 들어 "LightGray"로 변환된다는 것을 보여 주므로이 바인딩도 작동합니다. 그러나 UI에는 변화가 없으며 다른 바인딩은 직접 업데이트되므로 바인딩이 정상적으로 작동합니다.

UI가 'LightGray'로 업데이트되지 않는 이유를 모르지만 변환기가 올바르게 사용되고 있으며이 값을 전경 또는 배경색으로 반환하는 것처럼 보입니다.

+0

나는 또한 문제가 있지만 문제는 색상이나 브러쉬로 xaml을 바인딩하는 방법입니다. https://stackoverflow.com/questions/47546527/binding-background-color-of-style-from-a-class/47548263?noredirect = 1 # comment82059114_47548263 조금 도와 드릴 수 있습니다 –

답변

2

documentation이 보여 주듯이; ForegroundBrush이고 Color이 아님을 나타냅니다.

당신은 당신의 문제를 해결할 수 있습니다

var color = // Select your color here // 
var brush = new SolidColorBrush(color); 

기본적으로, 색상은 매우 자명하지만, 브러시는 뭔가를 그리는 실제 "자료"입니다.

+0

질문도 있지만 문제는 색상이나 브러시로 xaml을 바인딩하는 방법입니다 https://stackoverflow.com/questions/47546527/binding-background-color-of-style-from-a -class/47548263? noredirect = 1 # comment82059114_47548263 조금 도와 드릴 수 있습니다. –

관련 문제