2012-02-14 122 views
1

我正在使用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以獲得可摺疊列表框,並且代碼按照它應該的方式工作。 但是,這裏我不想要選擇功能。

任何人都可以請幫忙嗎?

謝謝你,布萊恩

+0

我看不到任何綁定到'toggleButtonModel' ... – 2012-02-14 13:54:15

+0

是的,那應該是'toggleButton'。複製了錯誤的輸出行。我編輯了這個問題。該綁定應該位於Groupbox的可見性屬性中。 – 2012-02-14 13:59:11

回答

1

你應該總是指定TargetType如果你知道的話,你的風格並不正確,因爲你使它通用通過不設置它,並在同一時間設置不存在的屬性適用容器因此被忽略。

更改TargetTypeContentPresenter,你將不再能夠設置一個Template

<ItemsControl.ItemContainerStyle> 
    <Style TargetType="ContentPresenter"> 
     <Setter Property="Template"> <!-- will throw an error --> 

你需要的一切移入ItemTemplate因爲沒有ControlTemplate進行設置。

+0

但它確實有效。我認爲他們在相同的範圍內,是不是ContentPresenter的目的?我不確定的更改在''行。當使用「ListBox」時,曾經是'''。 – 2012-02-14 14:40:05

+0

@BrianDay:ItemsControl中的容器應該是一個ContentPresenter而ListBox中的容器顯然是一個ListBoxItem。你甚至不應該改變模板,因爲ContentPresenters沒有這樣的屬性,模板將不會被應用,因此找不到該元素。 – 2012-02-14 14:54:17

+0

我明白了。代之以使用不帶複選框功能的「ListBox」結束。無論如何,非常感謝。 – 2012-02-14 17:26:40