【发布时间】:2012-01-12 05:17:24
【问题描述】:
TL;DR - 我遇到了绑定错误。疲倦的眼睛想念东西。
我已经使用 ListBox 作为容器实现了一个多选 CheckBox 列表。现在,在列表中的每个复选框旁边,我想显示一个可见性绑定到 ViewModel 属性的图像,但是我很难做到这一点。
我的风格是:
<Grid.Resources>
<Style x:Key="ListBoxCheckStyle" TargetType="ListBox">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBox">
<ItemsPresenter HorizontalAlignment="Left" VerticalAlignment="Top"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<ItemsPanelTemplate x:Key="ListBoxCheckStyleItemsPanelTemplate">
<StackPanel />
</ItemsPanelTemplate>
<Style x:Key="ListBoxItemCheckStyle" TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<StackPanel Orientation="Horizontal">
<ChimeControls:CheckBox
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
Margin="0,0,10,0"
IsChecked="{Binding IsSelected, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"/>
<Image
Width="16"
Height="16"
VerticalAlignment="Center"
Source="{StaticResource OccurredStatusTypeImageSource}"
Visibility="{Binding HasConsentCondition, Converter={StaticResource BoolToVisibilityConverter}, FallbackValue=Collapsed}"
HorizontalAlignment="Right"
Margin="10,0,10,0" />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
我的列表框定义为:
<ListBox
x:Name="objectivesListBox"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Style="{StaticResource ListBoxCheckStyle}"
ItemsPanel="{StaticResource ListBoxCheckStyleItemsPanelTemplate}"
ItemContainerStyle="{StaticResource ListBoxItemCheckStyle}"
ItemsSource="{Binding ObjectivesList}"
DisplayMemberPath="mgt_plan_obj_name"
AttachedProperties:ListBoxSelectedItems.Items="{Binding SelectedObjectives, Mode=TwoWay}"
SelectionMode="Multiple"/>
我的图像永远不会显示,并且 Visibility 绑定到的属性的 getter 永远不会被调用。我错过了什么?
【问题讨论】:
-
您的 XAML 看起来不错。运行时输出窗口中是否有任何绑定错误?如果你在你的
BoolToVisibilityConverter中加上一个 breapoint,它会被击中吗?OccurredStatusTypeImageSource是否指向正确的图像? -
呸,我可以发誓我查看了输出窗口,昨天没有绑定错误。我想这就是早上新鲜的眼睛带来的。感谢您的帮助。
标签: silverlight data-binding mvvm datatemplate