当我们打开新的时,折叠在“Datatemplate”中打开扩展器

时间:2014-01-20 12:41:55

标签: wpf windows-phone-7.1 expander

我的观点是这样的。我有一个Observable Collection,其中包含放在列表中的对象。通过单击任何项​​目,我可以打开与该项目相关的扩展器。这是一个问题:我怎么能 当我打开另一个时,崩溃(关闭)先前打开的扩展器?我不希望出现同时打开多个扩展器的情况。

我的WPF代码如下所示:

 <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <controls:Pivot>
            <controls:PivotItem>
                <ListBox x:Name="PizzaList" SelectionChanged="PizzaList_SelectionChanged" IsSynchronizedWithCurrentItem="false" >
                    <ListBox.ItemTemplate>
                        <DataTemplate x:Name="template">

                            <toolkit:ExpanderView Header="{Binding Name}" x:Name="expander" Style="{StaticResource ExpanderViewStyle}">
                                <toolkit:ExpanderView.Items>
                                   <!--first stack panel would contain all elements which would be showed 
                                    after clicking on listbox item-->
                                    <StackPanel Margin="20,0,0,0" Orientation="Vertical">
                  <!-- here is content of expander-->                    
                                    </StackPanel>
                                </toolkit:ExpanderView.Items>
                                <toolkit:ExpanderView.Expander>
                                    <TextBlock Text="{Binding Name_of_ingredients}" Width="500"></TextBlock>
                                </toolkit:ExpanderView.Expander>
                            </toolkit:ExpanderView>


                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </controls:PivotItem>
        </controls:Pivot>
    </Grid>

如果我使用固定数据集,我可以拥有静态数量的扩展器,但是当扩展器位于数据模板中时,项目数可能会发生变化。我不知道如何解决这个问题。

2 个答案:

答案 0 :(得分:2)

我认为这有助于:

private void PizzaListSelectionChanged(object sender, SelectionChangedEventArgs e)
{
    foreach (var item in PizzaList.Items)
    {
        var listBoxItem =
            PizzaList.ItemContainerGenerator.ContainerFromItem(item) as ListBoxItem;
        var itemExpander = (Expander) GetExpander(listBoxItem);
        if (itemExpander != null)
            itemExpander.IsExpanded = false;
    }
}

并搜索Expander

private static DependencyObject GetExpander(DependencyObject container)
{
    if (container is Expander) return container;

    for (var i = 0; i < VisualTreeHelper.GetChildrenCount(container); i++)
    {
        var child = VisualTreeHelper.GetChild(container, i);

        var result = GetExpander(child);
        if (result != null)
        {
            return result;
        }
    }

    return null;
}

在此问题中查找控件的更多方法 How can I find WPF controls by name or type?

答案 1 :(得分:0)

我希望这看起来不错,以前没有发布过代码。

我找到了扩展/折叠listboxitems的解决方案。下面的网格是我的内容。它的高度是stackpanel中的内容实际高度(x:Name =&#34; stack&#34;)并存储在Grid的Tag属性中。 DataTrigger为标记设置动画。习惯:XSButton来自Andy L的CustomControls,并充当ToggleButton。只需要这个转换器用于网格高度。

    public class MultiplyConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        double result = 1.0;
        for (int i = 0; i < values.Length; i++)
        {
            if (values[i] is double)
                result *= (double)values[i];
        }

        return result;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new Exception("Not implemented");
    }
}

网格

                <Grid x:Name="Grid" Grid.ColumnSpan="3" Grid.Row="1" >
                    <Grid.Tag>
                        <System:Double>0.0</System:Double>
                    </Grid.Tag>
                    <Grid.Height>
                        <MultiBinding Converter="{StaticResource multiplyConverter}">
                            <Binding Path="ActualHeight" ElementName="stack"/>
                            <Binding Path="Tag" RelativeSource="{RelativeSource Self}" />
                        </MultiBinding>
                    </Grid.Height>
                    <custom:XSButton IsChecked="{Binding 
                      RelativeSource={RelativeSource FindAncestor, 
                      AncestorType={x:Type ListBoxItem}},
                      Path=IsSelected}" Background="Transparent" BorderThickness="0" BorderBrush="Transparent"
                                      ClickMode="Press" 
                                    Foreground="Transparent" Style="{DynamicResource XSButtonStyle1}" >
                    <StackPanel x:Name="stack">
                        <TextBlock Text="{Binding Text}" FontSize="13.333" TextWrapping="Wrap"  Foreground="#FFC0C7C8"/>
                        <TextBlock Text="{Binding Status_Message}" FontSize="13.333"  TextWrapping="Wrap" Foreground="#FFC0C7C8"/>
                        </StackPanel>
                    </custom:XSButton>
                </Grid>

用于展开/折叠的DataTrigger

        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}}" Value="True">

                <DataTrigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetName="Grid" Storyboard.TargetProperty="Tag" To="1" Duration="0:0:0.4"/>
                        </Storyboard>
                    </BeginStoryboard>
                </DataTrigger.EnterActions>
                <DataTrigger.ExitActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetName="Grid" Storyboard.TargetProperty="Tag" To="0" Duration="0:0:0.4"/>
                        </Storyboard>
                    </BeginStoryboard>
                </DataTrigger.ExitActions>
            </DataTrigger>

        </DataTemplate.Triggers>