c# - 单击文本 block 时隐藏同级列表框

标签 c# wpf silverlight xaml windows-phone

我想知道是否有人知道如何在单击同级时更改 DataTemplate 中列表框的可见性。 DataTemplate 正在列表框上使用。以下是我正在使用的 xaml 示例:

 <DataTemplate x:Key="Template1">

                <StackPanel Margin="110,0,0,0">
                    <TextBlock Text="{Binding Name}" />
                    <TextBlock Name="ShowHide" Text="Hide" Tap="ShowHide_Tap" />
                    <ListBox Name="Listbox1" ItemsSource="{Binding SecondList}" Visibility="Visible" ItemTemplate="{StaticResource Template2}"/>
                </StackPanel>

        </DataTemplate>

以下是我的尝试,但我无法使用 FindName

 private void ShowHide_Click(object sender, System.Windows.Input.GestureEventArgs e)
    {


        var item = sender as TextBlock;
        ListBox Listbox = null;
        if (item != null)
        {

            ContentPresenter templateParent = GetFrameworkElementByName<ContentPresenter>(item);

            DataTemplate dataTemplate = templateParent.ContentTemplate;
            if (dataTemplate != null && templateParent != null)
            {
               Listbox = templateParent.FindName("Listbox1") as ListBox;
           }
            if (Listbox != null)
            {
                MessageBox.Show(String.Format("ERROR!"));
            }
            else
                Listbox.Visibility = Visibility.Collapsed;
        }


}

        private static T GetFrameworkElementByName<T>(FrameworkElement referenceElement) where T : FrameworkElement
        {
            FrameworkElement child = null;
            for (Int32 i = 0; i < VisualTreeHelper.GetChildrenCount(referenceElement); i++)
            {
                child = VisualTreeHelper.GetChild(referenceElement, i) as FrameworkElement;
                System.Diagnostics.Debug.WriteLine(child);
                if (child != null && child.GetType() == typeof(T))
                { break; }
                else if (child != null)
                {
                    child = GetFrameworkElementByName<T>(child);
                    if (child != null && child.GetType() == typeof(T))
                    {
                        break;
                    }
                }
            }
            return child as T;
        }

如果有人有任何见解,他们将不胜感激,

谢谢。

最佳答案

Blend SDK 恰好为此提供了功能——您甚至可以只使用 XAML,没有代码隐藏。只需使用 EventTrigger (在“点击”事件上)以及 ChangePropertyAction .这是它的样子:

<TextBlock Name="ShowHide" Text="Hide" >
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Tap">
            <ei:ChangePropertyAction TargetName="Listbox1" 
                PropertyName="Visibility" Value="Collapsed" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</TextBlock>
<ListBox Name="Listbox1" ItemsSource="{Binding SecondList}" Visibility="Visible" />

请注意,这需要您添加对以下扩展的引用:

  • System.Windows.Interactivity
  • Microsoft.Expression.Interactions

在 XAML 中使用命名空间引用它们:

  • xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
  • xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"

关于c# - 单击文本 block 时隐藏同级列表框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21492528/

相关文章:

c# - 从一种 PixelFormat 转换为另一种 WPF

WPF DataGrid EnableColumnVirtualization ="True"在 DataGridCellsPanel.get_HasCorrectRealizedColumns() 处引发 NullReferenceException

wcf - 带有 RIA WCF 项目的 Silverlight 无法添加常规 WCF 服务引用

c# - 将泛型类型转换为列表框控件

silverlight - 如何在 Blend 中诊断模糊的 XamlParseException?

c# - 如何为测试项目禁用 SonarLint

c# - 在 Windows 服务中捕获 'external drive inserted' 事件

c# - 使用 DesignInstance 将数据上下文绑定(bind)到 View

wpf - Entity Framework - 从数据库刷新对象

c# - 是否可以动态地将事件添加到 XAML 中的按钮?