Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 仅在选择ListViewItem时显示内容_C#_Wpf_Listbox_Selecteditem - Fatal编程技术网

C# 仅在选择ListViewItem时显示内容

C# 仅在选择ListViewItem时显示内容,c#,wpf,listbox,selecteditem,C#,Wpf,Listbox,Selecteditem,我有一个ListBox当选择一个ListBox项目时,我想更改按钮“View”的可见性并显示它。这意味着默认状态是隐藏的 这可能吗?如果可能的话,我是用XAML中的触发器还是代码隐藏中的触发器来解决这个问题 XAML块 <ListBox Background="Transparent" ItemContainerStyle="{StaticResource listBoxTemplate}" BorderThickness="0"> <ListBox.It

我有一个
ListBox
当选择一个
ListBox项目时,我想更改按钮“View”的可见性并显示它。这意味着默认状态是隐藏的

这可能吗?如果可能的话,我是用XAML中的触发器还是代码隐藏中的触发器来解决这个问题

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>

看法
[旧的无用代码]


编辑: 好吧,我没有把问题读得足够好,但这应该可以解决问题: 将
数据模板中的按钮替换为:

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

我建议您使用XAML触发器“方式”,因为它更干净…

对列表中的选定项使用转换器来确定可见性

<Button Visibility="{Binding ElementName=lb, Path=SelectedItem, Converter={StaticResource TestConverter}}" />

如果您使用的是MVVM模式,那么应该将按钮绑定到ICommand,例如来自的DelegateCommand。这样,按钮将使用命令的CanExecute()状态自行决定是否应启用它。

这将显示每个项目的所有按钮。按钮位于每个元素中。所以你的答案是错的:/是的,我试过了。但你没有抓住重点。这些按钮在列表框内重复。每个ListBoxItem由一个StackPanel组成。请看我的例子。如果你想显示列表中没有的一个按钮,你的例子就可以了。太棒了!这就成功了。想详细说明一下你在那里到底做了什么吗?也许可以指出这些绑定类型的一个很好的解释?我希望我的解释有意义!您需要检查SelectedItem是否与按钮所在的ListBoxItem相同……耶,我误解了这个问题。
<Button Visibility="{Binding ElementName=lb, Path=SelectedItem, Converter={StaticResource TestConverter}}" />
public class TestConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null)
            return Visibility.Collapsed;
        else
            return Visibility.Visible;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new Exception("The method or operation is not implemented.");
    }
}