基于wpf中的值在列表视图中启用/禁用按钮

时间:2019-02-05 07:51:45

标签: wpf listview

我有一个使用wpf中的stackpanel填充的listview。如果状态值设为0,我想隐藏pooja_name,否则它将可见。

<ListView x:Name="bookedlist" HorizontalAlignment="Left" Height="449" Margin="679,238,0,0" VerticalAlignment="Top" BorderBrush="#00828790" Background="Transparent" Focusable="False">
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel x:Name="stackkk" Orientation="Horizontal" >
                <Border BorderThickness="0.5" BorderBrush="#FFB0AEAE">
                    <TextBlock Text="{Binding Pooja_name}" TextAlignment="Left" Margin="5" Width="250"/>
                </Border>
                <Border BorderThickness="0.5" BorderBrush="#FFB0AEAE">
                    <TextBlock Text="{Binding Name}" TextAlignment="Left" Margin="5" Width="250"/>
                </Border>
                <Border BorderThickness="0.5" BorderBrush="#FFB0AEAE">
                    <TextBlock Text="{Binding Status}" TextAlignment="Left" MouseLeftButtonDown="Star_function" Margin="5" Width="95"/>
                </Border>
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

使用模型类填充列表视图

public class Booked
{
    public string Pooja_name { get; set; }
    public string Name { get; set; }
    public string Status{ get; set; }
}

和一个jsonarray

JArray bookedpoojalist = JArray.Parse(bookedval);
            List<Booked> booked = JsonConvert.DeserializeObject<List<Booked>>(bookedpoojalist.ToString());
            bookedlist.ItemsSource = booked;

2 个答案:

答案 0 :(得分:1)

您可以将StyleDataTrigger应用于BorderTextBlock元素:

<Border BorderThickness="0.5" BorderBrush="#FFB0AEAE">
    <TextBlock Text="{Binding Pooja_name}" TextAlignment="Left" Margin="5" Width="250"/>
    <Border.Style>
        <Style TargetType="Border">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Status}" Value="0">
                    <Setter Property="Visibility" Value="Collapsed" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Border.Style>
</Border>

答案 1 :(得分:-1)

这应该对您有用: 声明转换器:

<Window.Resources>
    <StringToVisibility x:Key="StringToVisibilityConverter"/>
</Window.Resources>

      <StackPanel x:Name="stackkk" Orientation="Horizontal">
        <Border BorderThickness="0.5" BorderBrush="#FFB0AEAE" Visibility="{Binding Status, Converter={StaticResource StringToVisibilityConverter}}">
            <TextBlock Text="{Binding Pooja_name}" TextAlignment="Left" Margin="5" Width="250"/>
         </Border>
      </StackPanel>

转换器:

    public class StringToVisibility : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value.ToString()== "0")
        {
            return Visibility.Collapsed;
        }
        return Visibility.Visible;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}