我正在使用C#和WPF,使用MVVM創建Windows應用程序。我正在嘗試創建可摺疊項目控件,以顯示來自集合的項目。展開每個項目時,應顯示包含項目屬性的分組框。我已在一個用戶控件內下列:wpf中的可摺疊項目控制 - 「System.Windows.Data Error:4」
<UserControl.Resources>
<SolidColorBrush x:Key="GlyphBrush" Color="#444" />
<ControlTemplate x:Key="toggleButtonTemplate" TargetType="ToggleButton">
<Grid
Width="15"
Height="13"
Background="Transparent">
<Path x:Name="ExpandPath"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Margin="1,1,1,1"
Fill="{StaticResource GlyphBrush}"
Data="M 4 0 L 8 4 L 4 8 Z"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked"
Value="True">
<Setter Property="Data"
TargetName="ExpandPath"
Value="M 0 4 L 8 4 L 4 8 Z"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<Style x:Key="toggleButtonStyle" TargetType="ToggleButton">
<Setter Property="Template" Value="{StaticResource toggleButtonTemplate}" />
</Style>
<BooleanToVisibilityConverter x:Key="VisibilityOfBool" />
<Style x:Key="CollapsibleListStyle" TargetType="{x:Type ItemsControl}">
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style>
<Setter Property="Control.Margin" Value="5" />
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate TargetType="ContentControl">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<ContentPresenter Grid.Column="0" Focusable="False">
</ContentPresenter>
<ToggleButton x:Name="toggleButton"
Grid.Column="1" IsChecked="False" Margin="3.5"
Style="{StaticResource toggleButtonStyle}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<WrapPanel>
<ItemsControl Name="itemsList"
Style="{StaticResource CollapsibleListStyle}"
ItemsSource="{Binding ViewModel.Items}"
Margin="0,0">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True" Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Name}" Grid.Column="0"
FontWeight="DemiBold" VerticalAlignment="Center"/>
<GroupBox Header="Properties" Grid.Column="1" Margin="5"
Visibility="{Binding ElementName=toggleButton,
Path=IsChecked, Converter={StaticResource VisibilityOfBool}}">
<WrapPanel HorizontalAlignment="Center">
<StackPanel Orientation="Horizontal" Margin="5">
<TextBlock Text="Code: "/>
<TextBlock Text="{Binding ItemCode}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="5">
<TextBlock Text="Key: "/>
<TextBlock Text="{Binding ItemKey}"/>
</StackPanel>
</WrapPanel>
</GroupBox>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</WrapPanel>
這在運行時產生以下錯誤:
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=toggleButton'. BindingExpression:Path=IsChecked; DataItem=null; target element is 'GroupBox' (Name=''); target property is 'Visibility' (type 'Visibility')
,因此不顯示的切換按鈕。 在我的應用程序的另一個地方,我已經使用了上面的內容,但用ListBox替換ItemsControl以獲得可摺疊列表框,並且代碼按照它應該的方式工作。 但是,這裏我不想要選擇功能。
任何人都可以請幫忙嗎?
謝謝你,布萊恩
我看不到任何綁定到'toggleButtonModel' ... – 2012-02-14 13:54:15
是的,那應該是'toggleButton'。複製了錯誤的輸出行。我編輯了這個問題。該綁定應該位於Groupbox的可見性屬性中。 – 2012-02-14 13:59:11