如何根据列表项的绑定值更改其视觉样式?
我有一个列表框(这是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您需要将项目网格的背景绑定到 DataContext 的值,并通过转换器传递它来实现您的逻辑。类似于
转换器的样子
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
where the converter will look like