2011-01-15 8 views
-3

ListViewItemの前景色をモデルのプロパティにバインドするにはどうすればよいですか?あなたの色のプロパティを見つける方法WPFがlistviewitemの前景色をバインドする

public class UserModel : BaseModel 
{ 
    public string UserName { get; private set; } 
    public int UserID { get; private set; } 
    public Brush Colour 
    { 
     get 
     { 
      return m_colour; 
     } 
     set 
     { 
      if (object.ReferenceEquals(m_colour, value)) 
       return; 

      m_colour = value; 
      OnPropertyChanged("Colour"); 
     } 
    } 

    private Brush m_colour = Brushes.Black; 

    public UserModel(int userID, string userName) 
    { 
     UserName = userName; 
     UserID = userID; 
    } 
} 

<ListView Name="lvClients" Grid.Column="0" Grid.Row="0" Margin="0,0,5,0" ItemsSource="{Binding Users, Mode=OneWay}" DisplayMemberPath="UserName" />

+3

が質問形式で何かを尋ねると、あなた自身の答えを投稿し完全に受け入れています。しかし、それを正しくしてください。実際に質問をし、実際にそれに答える。 –

+0

私はちょうど私かもしれないが、私は質問と無回答を参照してください...? – efdee

答えて

2

あなたの完全な構造に依存:

<ListView Name="lvClients" Grid.Column="0" Grid.Row="0" Margin="0,0,5,0" ItemsSource="{Binding Users, Mode=OneWay}" DisplayMemberPath="UserName"> 
     <ListView.ItemContainerStyle> 
      <Style TargetType="ListViewItem"> 
       <Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=Color}"/> 
      </Style> 
     </ListView.ItemContainerStyle> 
</ListView> 

それともあなただけのアイテムを持っていることになります直接ListViewForegroundに特異的に結合します同じ前景。

0

ItemTemplateを使用してください。例えば、これに

<Window.Resources> 

<DataTemplate x:Key="myTemplate"> 
    <StackPanel Background={Binding Colour}> 
    <TextBlock Text="{Binding Path=UserName}" /> 
    </StackPanel> 
</DataTemplate> 

</Window.Resources> 

<ListBox Width="400" Margin="10" 
     ItemsSource="{Binding Users}" 
     ItemTemplate="{StaticResource myTemplate}"/> 

詳細情報はここで見つけることができます:http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.itemtemplate.aspx