2011-05-07 6 views
0

관찰 가능한 컬렉션에 묶여있는 목록 상자가 있습니다. 컬렉션의 요소에는 color라는 변수가 포함되어 있습니다. 내 목록 상자의 항목은 이미 컬렉션에 바인딩되어 있지만 항목 글꼴 색상을 바인딩하는 방법은 무엇입니까? 나는 이미이목록 상자 (wpf)에서 항목의 글꼴 색상을 변경하는 방법

<DataTemplate x:Key="myListBox"> 
     <TextBlock Padding="0,0,10,0" 
    Text="{Binding Path=Color, Mode=Default}"/> 
    </DataTemplate> 

같은 색상 이름으로 미세 교체 항목의 이름을 작동하는 데이터 템플릿을 가지고 있지만 나는 색상을 결합하기 위해 설정해야하는 속성을 찾을 수 없습니다.

답변

2

어떤 색상을 사용하고 있는지 알 수는 없지만 배경과 텍스트/전경색이 설정됩니다.

<TextBlock Padding="0,0,10,0" 
    Text="{Binding Path=Color, Mode=Default}" 
    Background="{Binding myBackgroundColour}" 
    Foreground="{Binding myTextColour}" 
/> 

편집 : 의존성 소품 -

public string Color 
{ 
    get { return (string)GetValue(ColorProperty); } 
    set { SetValue(ColorProperty, value); } 
} 

// Using a DependencyProperty as the backing store for Color. This enables animation, styling, binding, etc... 
public static readonly DependencyProperty ColorProperty = 
    DependencyProperty.Register("Color", typeof(string), typeof(CLASSNAMEHERE), new UIPropertyMetadata("Black")); 

는 뷰 모델 클래스 또는 코드 숨김 클래스 이름, 즉, 당신이 그것을에두고있는 클래스 이름으로 CLASSNAMEHERE를 교체합니다.

사용 :

this.Color = "Yellow"; 
+0

를 사용할 수 있습니까? – user579674

+0

색상을 바인딩하려는 속성입니다. 귀하의 예에 따라 텍스트의 색상을 설정하고 배경을 설정하지 않은 채로 두십시오. (색상이 흰색 인 경우 ...) –

+0

이미이 줄 Foreground = "{Binding Path = Color}"하지만 작동하지 않으며 않습니다. 왜 그런지 안다. 예를 들어 전경 = "노란색"을 사용하면 정상적으로 작동합니다. – user579674

1

는 정확히 myBackgroundColour/myTextColour 무엇이 자원 스타일

<Style TargetType="{x:Type ListBoxItem}"> 
     <Setter Property="Foreground" Value="></Setter> 
     <Setter Property="FontWeight" Value="Bold"></Setter> 
     bla bla bla 
    </Style> 
관련 문제