WPF绑定listviewitem的前景色

发布于 2024-10-12 10:45:32 字数 806 浏览 8 评论 0原文

如何将 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;
    }
}

How would i bind the foreground colour of a ListViewItem to a property of a model?

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" />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吃素的狼 2024-10-19 10:45:32

如何找到颜色属性取决于您的完整结构:

<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 ,这将导致项目具有相同的前景。

How you find your color property depends on your full structure:

<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>

Or you just bind to the Foreground of the ListView directly which will cause the items to have the same foreground.

街道布景 2024-10-19 10:45:32

您应该使用 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

You should use an ItemTemplate. For instance

<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}"/>

More info on this can be found here: http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.itemtemplate.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文