如何根据列表项的绑定值更改其视觉样式?

发布于 2024-09-04 22:34:07 字数 2362 浏览 5 评论 0原文

我有一个列表框(这是 xaml):

            <ListBox MinWidth="300" ItemsSource="{Binding Relationships, Mode=OneWay}" 
        SelectedItem="{Binding SelectedRelationship, Mode=TwoWay}" SelectionMode="Single" 
        HorizontalAlignment="Left" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                            <RowDefinition/>
                            <RowDefinition/>
                            <RowDefinition/>
                            <RowDefinition/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>
                        <CheckBox IsChecked = "{Binding IsPrimary}" IsHitTestVisible="False" />
                        <StackPanel Orientation="Horizontal" Grid.Column="1">
                            <TextBlock Text="{Binding RelationshipType}"  FontWeight="Bold"  Margin="0,0,5,0" />
                            <TextBlock Text="{Binding Status}"  FontStyle="Italic" />                           
                        </StackPanel>
                        <TextBlock Text="{Binding UnitName}" Grid.Row="1" Grid.Column="1" />
                        <TextBlock Text="{Binding StartDate, Converter={StaticResource DateConverter}}" Grid.Row="2" Grid.Column="1"/>
                        <TextBlock Text="{Binding RetireDate}" Grid.Row="3" Grid.Column="1" />
                        <TextBlock Text="{Binding EndDate}" Grid.Row="4" Grid.Column="1" />
                        <TextBlock Text="{Binding ReasonForLeaving}" Grid.Row="5" Grid.Column="1" />
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

我想要做的是让列表框中的每个项目都有 3 个背景之一(如果 IsPrimary = true 则为绿色,如果 EndDate 值为空则为橙色,如果 EndDate 值为灰色)不为空。

没有办法对列表框项目进行模板化,以便它们评估绑定项目以确定视图状态或将每个列表框项目绑定到我可以为视图模型中的每个项目设置的值?

I have a listbox (here's the xaml):

            <ListBox MinWidth="300" ItemsSource="{Binding Relationships, Mode=OneWay}" 
        SelectedItem="{Binding SelectedRelationship, Mode=TwoWay}" SelectionMode="Single" 
        HorizontalAlignment="Left" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                            <RowDefinition/>
                            <RowDefinition/>
                            <RowDefinition/>
                            <RowDefinition/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>
                        <CheckBox IsChecked = "{Binding IsPrimary}" IsHitTestVisible="False" />
                        <StackPanel Orientation="Horizontal" Grid.Column="1">
                            <TextBlock Text="{Binding RelationshipType}"  FontWeight="Bold"  Margin="0,0,5,0" />
                            <TextBlock Text="{Binding Status}"  FontStyle="Italic" />                           
                        </StackPanel>
                        <TextBlock Text="{Binding UnitName}" Grid.Row="1" Grid.Column="1" />
                        <TextBlock Text="{Binding StartDate, Converter={StaticResource DateConverter}}" Grid.Row="2" Grid.Column="1"/>
                        <TextBlock Text="{Binding RetireDate}" Grid.Row="3" Grid.Column="1" />
                        <TextBlock Text="{Binding EndDate}" Grid.Row="4" Grid.Column="1" />
                        <TextBlock Text="{Binding ReasonForLeaving}" Grid.Row="5" Grid.Column="1" />
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

What I want to do is have each item in the listbox have one of 3 backgrounds (green if the value of IsPrimary = true, Orange if the EndDate value is empty and grey if the EndDate value is not empty.

Is there a way to template the listbox items so that they evaluate bound items to determine a view state or to have each listbox item bind to a value that I can set for each item in my viewmodel?

Thanks for your help.

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

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

发布评论

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

评论(1

離殇 2024-09-11 22:34:07

我认为您需要将项目网格的背景绑定到 DataContext 的值,并通过转换器传递它来实现您的逻辑。类似于

<Grid Background="{Binding Converter={StaticResource myItemConverter}}">
   ...
</Grid>

转换器的样子

public class MyItemConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var item = value as ItemModel; // [Your ListboxItem Class];
        if (item.IsPrimary)
            return System.Windows.Media.Brushes.Green;
        return item.EndDate.HasValue ? 
            System.Windows.Media.Brushes.Gray : 
            System.Windows.Media.Brushes.Orange;
    }
}

I think you need to bind the background of the item's Grid to the value of the DataContext, and pass it through a converter to implement your logic. Something like

<Grid Background="{Binding Converter={StaticResource myItemConverter}}">
   ...
</Grid>

where the converter will look like

public class MyItemConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var item = value as ItemModel; // [Your ListboxItem Class];
        if (item.IsPrimary)
            return System.Windows.Media.Brushes.Green;
        return item.EndDate.HasValue ? 
            System.Windows.Media.Brushes.Gray : 
            System.Windows.Media.Brushes.Orange;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文