How to disable some elements in XAML for WPF ListView

OK, sorry for the too broad question, but let's see what you guys have to offer ....

I have a ListView WPF loaded by an XML file using XAML (code below)

I have a second XML file with elements that match what is in my ListView. However, if there is no match in the second file, I want ListItem to be disabled.

A simple example:

My ListView has:

                   Joe
                   Fred
                   Jim  

(because it was loaded with the first XML file)

My second XML file has (essentially):

                  Joe
                  Jim

I want ListView to somehow consume this second file, resulting in "Fred" being disabled.

I guess it will be some kind of β€œfilter” that I would apply somewhere in XAML.

<ListView Name="lvwSourceFiles" 
          Margin="11,93,0,12" VerticalContentAlignment="Center" 
          HorizontalAlignment="Left" Width="306"
          Cursor="Hand" TabIndex="6" 
          ItemsSource="{Binding}"
          SelectionMode="Multiple"
          SelectionChanged="lvwSourceFiles_SelectionChanged" >
    <ListBox.DataContext>
        <XmlDataProvider x:Name="xmlSourceFiles" XPath="AssemblyUpdaterSource/sources/source/File" />
    </ListBox.DataContext>
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <EventSetter Event="PreviewMouseRightButtonDown"
                         Handler="OnSourceListViewItemPreviewMouseRightButtonDown" />
        </Style>
    </ListView.ItemContainerStyle>
</ListView>
+1
2

, , XAML. , ListView.Loaded, . , ListView , .

, , XAML, , , . , XmlDataProvider, ; , , XML.

:

public partial class Window2 : Window
{
    private List<Person> _persons = new List<Person>();

    public Window2()
    {
        InitializeComponent();

        _persons.Add(new Person("Joe"));
        _persons.Add(new Person("Fred"));
        _persons.Add(new Person("Jim"));
    }

    public List<Person> Persons
    {
        get { return _persons; }
    }

    public static List<Person> FilterList
    {
        get
        {
            return new List<Person>()
            {
                new Person("Joe"), 
                new Person("Jim")
            };
        }
    }
}

public class Person
{
    string _name;

    public Person(string name)
    {
        _name = name;
    }

    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }

    public override string ToString()
    {
        return _name;
    }
}   

Person, Name.

, XAML:

<Window x:Class="TestWpfApplication.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestWpfApplication"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="Window2" Height="300" Width="300"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Window.Resources>
    <local:PersonInListConverter x:Key="personInListConverter"/>
    <ObjectDataProvider x:Key="filterList" ObjectInstance="{x:Static local:Window2.FilterList}"/>
</Window.Resources>
<StackPanel>
    <ListView ItemsSource="{Binding Persons}"
              SelectionMode="Multiple"
              Name="lvwSourceFiles" Cursor="Hand" VerticalContentAlignment="Center">
        <ListView.ItemContainerStyle>
            <Style TargetType="{x:Type ListViewItem}">
                <Setter Property="IsEnabled" 
                        Value="{Binding Name, Converter={StaticResource personInListConverter}, ConverterParameter={StaticResource filterList}}"/>
            </Style>
        </ListView.ItemContainerStyle>
    </ListView>
</StackPanel>

IsEnabled ListViewItem Name Person. , , . ConverterParameter FilterList, XML. , :

public class PersonInListConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string name = (string)value;
        List<Person> persons = (parameter as ObjectDataProvider).ObjectInstance as List<Person>;

        return persons.Exists(person => name.Equals(person.Name));
    }

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

:

alt text

+6

, dataTemplate . datatrigger, , - items, , XML .

, xml , prop Exists ,

, , , - isenabled , .

+3

Source: https://habr.com/ru/post/1717122/


All Articles