2013-01-10 33 views
0

我创建扩展的列表框这样的问题,单选按钮:Expander Auto Open/Close列表框扩展的不像个

解决方案的工作与在扩展内容时,列表框是窗口上的唯一项目。

但是,当我试图用我的自定义控件使用相同的代码,我得到这个错误:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ListBoxItem', AncestorLevel='1''. BindingExpression:Path=IsSelected; DataItem=null; target element is 'Expander' (Name=''); target property is 'IsExpanded' (type 'Boolean')

我已经尝试添加IsSelected属性在ListBox.ItemContainerStyle作为一个海报中建议另一个线程但失败了。

<ListBox Margin="5" 
     SelectionMode="Single" 
     ScrollViewer.VerticalScrollBarVisibility="Auto"> 
    <ListBox.Resources> 
     <Style TargetType="{x:Type Expander}"> 
      <Setter Property="IsExpanded" 
        Value="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}" /> 
     </Style> 
     <Style TargetType="{x:Type controls:SelectionCriteriaControl}"> 
      <Setter Property="MaxHeight" 
        Value="200" /> 
     </Style> 
    </ListBox.Resources> 
    <Expander Header="Fund Family" Margin="2"> 
     <StackPanel> 
      <controls:SelectionCriteriaControl DataContext="{Binding FundFamilySelectionCriteria, Mode=TwoWay}" /> 
     </StackPanel> 
    </Expander> 
    <Expander Header="Asset Class" Margin="2"> 
     <StackPanel> 
      <controls:SelectionCriteriaControl DataContext="{Binding AssetClassSelectionCriteria, Mode=TwoWay}" /> 
     </StackPanel> 
    </Expander> 
    <ListBox.Template> 
     <ControlTemplate TargetType="{x:Type ListBox}"> 
      <ItemsPresenter /> 
     </ControlTemplate> 
    </ListBox.Template> 
    <ListBox.ItemContainerStyle> 
     <Style TargetType="{x:Type ListBoxItem}"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type ListBoxItem}"> 
         <ContentPresenter Content="{TemplateBinding Content}" /> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </ListBox.ItemContainerStyle> 
</ListBox> 

悲惨的失败了!!!!!

任何帮助表示赞赏:)

+0

它是如何失败?你在屏幕上看到什么? – XAMeLi

+0

扩展器应该是相互排斥的,就像分组的单选按钮。如果你点击一个,另一个打开,第一个应该关闭。如果用TextBlock替换嵌入在StackPanels中的UserControl(例如),它就可以工作。 –

回答

2

这是一个有点大场景为我建立的那一刻,但一些想到尝试:

Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsSelected}" /> 

我不认为其中使用了可以按类型定义相关源的模板。

编辑:此代码工作正常:根据您的原始,TemplatedParent是没有必要的。

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition/> 
     <RowDefinition Height="auto"/> 
    </Grid.RowDefinitions> 
    <ListBox x:Name="testList" Margin="5" 
    SelectionMode="Single" 
    ScrollViewer.VerticalScrollBarVisibility="Auto"> 
     <ListBox.Resources> 
      <Style TargetType="{x:Type Expander}"> 
       <Setter Property="IsExpanded" 
       Value="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}" /> 
      </Style> 
     </ListBox.Resources> 
     <Expander Header="Fund Family" Margin="2"> 
      <StackPanel> 
       <TextBlock Text="First"/> 
       <TextBlock Text="Second"/> 
      </StackPanel> 
     </Expander> 
     <Expander Header="Asset Class" Margin="2"> 
      <StackPanel> 
       <TextBlock Text="First"/> 
       <TextBlock Text="Second"/> 
      </StackPanel> 
     </Expander> 
     <ListBox.Template> 
      <ControlTemplate TargetType="{x:Type ListBox}"> 
       <ItemsPresenter /> 
      </ControlTemplate> 
     </ListBox.Template> 
     <ListBox.ItemContainerStyle> 
      <Style TargetType="{x:Type ListBoxItem}"> 
       <Setter Property="Template"> 
        <Setter.Value> 
         <ControlTemplate TargetType="{x:Type ListBoxItem}"> 
          <ContentPresenter Content="{TemplateBinding Content}" /> 
         </ControlTemplate> 
        </Setter.Value> 
       </Setter> 
      </Style> 
     </ListBox.ItemContainerStyle> 
    </ListBox> 
    <Button HorizontalAlignment="Left" Content="Test" Grid.Row="1" Width="60" Click="Button_Click"/> 
</Grid> 

和快速代码隐藏触发程序SelectedIndex集...

 private void Button_Click(object sender, RoutedEventArgs e) 
    { 
     testList.SelectedIndex = 1; 
    } 

似乎为我工作的罚款。点击列表项可以展开,甚至可以使用按钮通过设置扩展的选定索引来设置它。东西很腥是影响你的具体情况...:]

+0

如果我将UserControl替换为TextBox,模板会按预期工作,这是什么让我疯狂。有什么我需要处理,我不是? –

+0

不幸的是,这并没有工作:( –

+0

我已经对你的代码有点玩了,我想你正在追逐一些吉普赛货车。我把你的代码粘贴到一个测试项目中,用你的代码替换你的用户控件一对夫妇的文本块,甚至添加了一个按钮,在列表框中设置一个'SelectedIndex',并且展开器展开后没有错误。我认为你还有其他的东西正在进行中......我用控件修改了我的答案I已添加,将其弹出到窗口中,以便在出现某些特定环境问题时进行尝试...... –