<i id='MvpLT'><tr id='MvpLT'><dt id='MvpLT'><q id='MvpLT'><span id='MvpLT'><b id='MvpLT'><form id='MvpLT'><ins id='MvpLT'></ins><ul id='MvpLT'></ul><sub id='MvpLT'></sub></form><legend id='MvpLT'></legend><bdo id='MvpLT'><pre id='MvpLT'><center id='MvpLT'></center></pre></bdo></b><th id='MvpLT'></th></span></q></dt></tr></i><div id='MvpLT'><tfoot id='MvpLT'></tfoot><dl id='MvpLT'><fieldset id='MvpLT'></fieldset></dl></div>

    <bdo id='MvpLT'></bdo><ul id='MvpLT'></ul>
    <tfoot id='MvpLT'></tfoot>

        <small id='MvpLT'></small><noframes id='MvpLT'>

        <legend id='MvpLT'><style id='MvpLT'><dir id='MvpLT'><q id='MvpLT'></q></dir></style></legend>
      1. 仅在选择 ListViewItem 时显示内容

        Displaying Content only when ListViewItem is Selected(仅在选择 ListViewItem 时显示内容)
          <legend id='eFvEB'><style id='eFvEB'><dir id='eFvEB'><q id='eFvEB'></q></dir></style></legend>
          • <bdo id='eFvEB'></bdo><ul id='eFvEB'></ul>
          • <i id='eFvEB'><tr id='eFvEB'><dt id='eFvEB'><q id='eFvEB'><span id='eFvEB'><b id='eFvEB'><form id='eFvEB'><ins id='eFvEB'></ins><ul id='eFvEB'></ul><sub id='eFvEB'></sub></form><legend id='eFvEB'></legend><bdo id='eFvEB'><pre id='eFvEB'><center id='eFvEB'></center></pre></bdo></b><th id='eFvEB'></th></span></q></dt></tr></i><div id='eFvEB'><tfoot id='eFvEB'></tfoot><dl id='eFvEB'><fieldset id='eFvEB'></fieldset></dl></div>
                <tbody id='eFvEB'></tbody>

            1. <tfoot id='eFvEB'></tfoot>

                <small id='eFvEB'></small><noframes id='eFvEB'>

                  本文介绍了仅在选择 ListViewItem 时显示内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  I have a ListBox when one of the ListBoxItems are selected I want to change the visibility of the button "View" and display it.意味着默认状态是隐藏.

                  I have a ListBox when one of the ListBoxItems are selected I want to change the visibility of the button "View" and display it. Meaning that the default state is Hidden.

                  这可能吗?如果可以,我是否可以使用 XAML 中的触发器或代码隐藏来解决此问题?

                  Is this possible and if so, do I solve this with a trigger in XAML or in code behind?

                  XAML 片段

                  <ListBox Background="Transparent" 
                        ItemContainerStyle="{StaticResource listBoxTemplate}" BorderThickness="0">
                      <ListBox.ItemsPanel>
                          <ItemsPanelTemplate>
                              <StackPanel Orientation="Vertical" VerticalAlignment="Center" />
                          </ItemsPanelTemplate>
                      </ListBox.ItemsPanel>
                      <ListBox.ItemTemplate>
                          <DataTemplate>
                              <Grid Background="Beige" Margin="10 10 10 10" 
                                      HorizontalAlignment="Center" Width="500" Height="100"
                                      VerticalAlignment="Stretch">
                                  <Grid.RowDefinitions>
                                      <RowDefinition Name="TopRow" Height="50" />
                                      <RowDefinition Name="BottomRow" Height="*" />
                                  </Grid.RowDefinitions>
                                  <Grid.ColumnDefinitions>
                                      <ColumnDefinition Name="LeftSideMenu" Width="*"/>
                                      <ColumnDefinition Name="Middle" Width="*"/>
                                  </Grid.ColumnDefinitions>
                                  <Label Grid.Column="0" Grid.Row="0" Content="{Binding name}" />
                  
                                  <Button Grid.Column="1" VerticalAlignment="Center" 
                                      Grid.RowSpan="2" Name="view" Click="viewClicked_Click"
                                      Grid.Row="0">View</Button>
                  
                                  <Label Grid.Column="0" Grid.Row="1" 
                                      Content="{Binding description}" />
                              </Grid>
                          </DataTemplate>
                      </ListBox.ItemTemplate>
                  </ListBox>
                  

                  推荐答案

                  【老的没用的代码】

                  好吧,我没有很好地阅读这个问题,但这应该可以解决问题:将 DataTemplate 中的按钮替换为:

                  Well I didn't read the question good enough but this should do the trick: Replace the button in your DataTemplate with this:

                  <Button Grid.Row="0" Grid.Column="1" Grid.RowSpan="2" VerticalAlignment="Center"
                          Name="view" Click="viewClicked_Click"
                          Content="View">
                   <Button.Style>
                    <Style TargetType="{x:Type Button}">
                     <Setter Property="Visibility" Value="Collapsed"/>
                     <Style.Triggers>
                      <DataTrigger Binding="{Binding 
                                             RelativeSource={RelativeSource Mode=FindAncestor,
                                             AncestorType={x:Type ListBoxItem}},Path=IsSelected}" 
                                   Value="True">
                       <Setter Property="Visibility" Value="Visible"/>
                      </DataTrigger>
                     </Style.Triggers>
                    </Style>
                   </Button.Style>
                  </Button>
                  

                  这里发生的事情是样式将 Visibility 属性设置为 Collapsed 并使用触发器来更改它.我使用了 DataTrigger,因此您可以使用 {Binding ...}.使用 {Binding RelativeSource={..}} 您可以查找 Ancestor,这里我们正在查找类型为 ListBoxItem 的 Ancestor.

                  What happends here is that the style sets the Visibility property to Collapsed and uses a trigger to change that. I've used a DataTrigger so you can use the {Binding ...}. With the {Binding RelativeSource={..}} you can look for an Ancestor, here we are looking for an Ancestor of type ListBoxItem.

                  这样做是在 WPF 树中向上"查找,以查看此按钮是否具有类型为 ListBoxItem 的 Parent 对象,如果找到,它将检查 IsSelected (Path=IsSelected) 属性,如果它是 True,然后将更新按钮的 Visibility 属性.

                  What this does is look 'up' in the WPF tree to see if this button has a Parent object which is of type ListBoxItem if it's found it will check the IsSelected (Path=IsSelected) property if it's True and will then update the Visibility Property of the button.

                  我希望这个解释是有道理的!:-)

                  I hope this explanation makes sense! :-)

                  只是为了好玩,背后的代码方式:

                  Just for fun, the code behind way:

                  private Button _previousButton;
                  private void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
                  {
                   if (_previousButton != null)
                    _previousButton.Visibility = Visibility.Collapsed;
                  
                   // Make sure an item is selected
                   if (listBox.SelectedItems.Count == 0)
                    return;
                  
                   // Get the first SelectedItem (use a List<object> when 
                   // the SelectionMode is set to Multiple)
                   object selectedItem = listBox.SelectedItems[0];
                   // Get the ListBoxItem from the ContainerGenerator
                   ListBoxItem listBoxItem = listBox.ItemContainerGenerator.ContainerFromItem(selectedItem) as ListBoxItem;
                   if (listBoxItem == null)
                    return;
                  
                   // Find a button in the WPF Tree
                   Button button = FindDescendant<Button>(listBoxItem);
                   if (button == null)
                    return;
                  
                   button.Visibility = Visibility.Visible;
                   _previousButton = button;
                  }
                  
                  /// <summary>
                  /// Finds the descendant of a dependency object.
                  /// </summary>
                  /// <typeparam name="T"></typeparam>
                  /// <param name="obj">The obj.</param>
                  /// <returns></returns>
                  public static T FindDescendant<T>(DependencyObject obj) where T : DependencyObject
                  {
                   // Check if this object is the specified type
                   if (obj is T)
                    return obj as T;
                  
                   // Check for children
                   int childrenCount = VisualTreeHelper.GetChildrenCount(obj);
                   if (childrenCount < 1)
                    return null;
                  
                   // First check all the children
                   for (int i = 0; i < childrenCount; i++)
                   {
                    DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                    if (child is T)
                     return child as T;
                   }
                  
                   // Then check the childrens children
                   for (int i = 0; i < childrenCount; i++)
                   {
                    DependencyObject child = FindDescendant<T>(VisualTreeHelper.GetChild(obj, i));
                    if (child != null && child is T)
                     return child as T;
                   }
                  
                   return null;
                  }
                  

                  我建议你使用 XAML 触发器方式",因为它更干净...

                  I suggest you use the XAML Trigger "way" because it's much cleaner...

                  这篇关于仅在选择 ListViewItem 时显示内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Populate ListBox with a IEnumrable on another thread (winforms)(在另一个线程(winforms)上使用 IEnumrable 填充 ListBox)
                  listbox selected item give me quot; System.Data.DataRowViewquot; , C# winforms(列表框选择的项目给我quot;System.Data.DataRowView, C# Winforms)
                  Cannot remove items from ListBox(无法从列表框中删除项目)
                  Preventing ListBox scrolling to top when updated(更新时防止列表框滚动到顶部)
                  Drag and drop from list to canvas on windows phone with MVVM(使用 MVVM 在 Windows 手机上从列表拖放到画布)
                  Deselection on a WPF listbox with extended selection mode(具有扩展选择模式的 WPF 列表框上的取消选择)
                  <legend id='EPKDU'><style id='EPKDU'><dir id='EPKDU'><q id='EPKDU'></q></dir></style></legend>
                1. <small id='EPKDU'></small><noframes id='EPKDU'>

                    <i id='EPKDU'><tr id='EPKDU'><dt id='EPKDU'><q id='EPKDU'><span id='EPKDU'><b id='EPKDU'><form id='EPKDU'><ins id='EPKDU'></ins><ul id='EPKDU'></ul><sub id='EPKDU'></sub></form><legend id='EPKDU'></legend><bdo id='EPKDU'><pre id='EPKDU'><center id='EPKDU'></center></pre></bdo></b><th id='EPKDU'></th></span></q></dt></tr></i><div id='EPKDU'><tfoot id='EPKDU'></tfoot><dl id='EPKDU'><fieldset id='EPKDU'></fieldset></dl></div>
                      <tbody id='EPKDU'></tbody>
                      <bdo id='EPKDU'></bdo><ul id='EPKDU'></ul>
                    • <tfoot id='EPKDU'></tfoot>