显示只有当ListViewItem的选择了内容 [英] Displaying Content only when ListViewItem is Selected

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

问题描述

我有一个的ListBox 当一个 ListBoxItems 选择我想更改按钮的可见性查看,显示它。这意味着默认状态是隐藏的。



这是可能的,如果是这样,我解决了这个在XAML或代码后面?

$ B触发
$ b

XAML片

 <列表框背景=透明 
ItemContainerStyle ={StaticResource的listBoxTemplate}了borderThickness =0>
< ListBox.ItemsPanel>
< ItemsPanelTemplate>
< StackPanel的方向=垂直VerticalAlignment =中心/>
< / ItemsPanelTemplate>
< /ListBox.ItemsPanel>
< ListBox.ItemTemplate>
<&DataTemplate的GT;
<网格背景=米色保证金=10 10 10 10
的Horizo​​ntalAlignment =中心WIDTH =500HEIGHT =100
VerticalAlignment =拉伸>
< Grid.RowDefinitions>
< RowDefinition NAME =TopRowHEIGHT =50/>
< RowDefinition NAME =BottomRowHEIGHT =*/>
< /Grid.RowDefinitions>
< Grid.ColumnDefinitions>
< ColumnDefinition NAME =LeftSideMenuWIDTH =*/>
< ColumnDefinition名称=中WIDTH =*/>
< /Grid.ColumnDefinitions>
<标签Grid.Column =0Grid.Row =0CONTENT ={结合}名/>

<按钮Grid.Column =1VerticalAlignment =中心
Grid.RowSpan =2NAME =查看点击=viewClicked_Click
网格。行=0>查看< /按钮>

将;标签Grid.Column =0Grid.Row =1
含量={结合描述}/>
< /网格和GT;
< / DataTemplate中>
< /ListBox.ItemTemplate>
< /列表框>


解决方案

[老无用代码]






编辑:
嗯,我没看过这个问题不够好,但这个应该做的伎俩:
更换按钮在你的的DataTemplate 与此:

 <按钮Grid.Row = 0Grid.Column =1Grid.RowSpan =2VerticalAlignment =中心
名称=查看点击=viewClicked_Click
含量=查看>
< Button.Style>
<风格的TargetType ={X:类型按钮}>
< setter属性=能见度VALUE =折叠/>
< Style.Triggers>
< DataTrigger绑定={结合
的RelativeSource = {的RelativeSource模式= FindAncestor,
AncestorType = {X:输入一个ListBoxItem}},路径= IsSelected}
值=真正的>
< setter属性=能见度VALUE =可见/>
< / DataTrigger>
< /Style.Triggers>
< /样式和GT;
< /Button.Style>
< /按钮>



什么happends这里要说的是风格设置Visibility属性来折叠,并使用触发器来改变这种状况。我已经使用了 DataTrigger ,所以你可以使用 {绑定...}
使用 {绑定的RelativeSource = {..}} 你可以找一个祖先,在这里我们正在寻找的类型的祖先 ListBoxItem中



这样做是在WPF树,看看这个按钮有一个父对象,它是类型看了 ListBoxItem的如果它发现它会检查IsSelected(路径= IsSelected )财产,如果这是真的,然后将更新按钮的可见性属性。



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






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

 个人按钮_previousButton; 
私人无效listBox_SelectionChanged(对象发件人,SelectionChangedEventArgs E)
{
如果(_previousButton!= NULL)
_previousButton.Visibility = Visibility.Collapsed;

//确保一个项目被选中
如果(listBox.SelectedItems.Count == 0)
的回报;

//获取第一的SelectedItem(使用列表<对象>在
//将SelectionMode设置为多)
对象将selectedItem = listBox.SelectedItems [0];
//从ContainerGenerator
ListBoxItem的一个ListBoxItem = listBox.ItemContainerGenerator.ContainerFromItem(将selectedItem)作为一个ListBoxItem的ListBoxItem的;
如果(ListBoxItem的== NULL)
的回报;

//查找在WPF树
按钮一个按钮= FindDescendant<按钮和GT;(ListBoxItem的);
如果(键== NULL)
的回报;

button.Visibility = Visibility.Visible;
_previousButton =按钮;
}

///<总结>
///查找依赖对象的后代。
///< /总结>
///< typeparam NAME =T>< / typeparam>
///< PARAM NAME =目标文件>在OBJ< /参数>
///<&回报GT;< /回报>
公共静态牛逼FindDescendant< T>(DependencyObject的OBJ)其中T:DependencyObject的
{
//检查是否这个对象是指定类型
如果(obj为T)
返回OBJ为T;

//检查儿童
INT childrenCount = VisualTreeHelper.GetChildrenCount(OBJ);
如果(childrenCount< 1)
返回NULL;

//首先检查所有的孩子
的for(int i = 0; I< childrenCount;我++)
{
DependencyObject的孩子= VisualTreeHelper.GetChild(OBJ , 一世);
如果(孩子T)
返回子为T;
}

//然后检查儿童儿童
的for(int i = 0; I< childrenCount;我++)
{
DependencyObject的孩子= FindDescendant< T>(VisualTreeHelper.GetChild(OBJ,I));
如果(孩子= NULL&放大器;!&安培;孩子是T)
返回子为T;
}

返回NULL;
}



我建议你使用XAML触发器路,因为它是干净多了..


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.

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

XAML Piece

<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>

解决方案

[Old useless code]


EDIT: 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>

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.

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! :-)


EDIT2: 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;
}

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆