如何禁用XAML一些项目的ListView的WPF [英] How to disable some items in XAML for a WPF ListView

查看:339
本文介绍了如何禁用XAML一些项目的ListView的WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OK,比较遗憾的是过于宽泛的问题,但让我们看看你们的建议....

我有一个XML文件加载一个WPF的ListView,使用XAML(code以下)

我有匹配的内容是我的ListView的项目第二个XML文件。但是,如果是的的在第二个文件中的匹配,那么我想那个列表项禁用。

一个简单的例子:

我的ListView中有:

 
                   弗雷德
                   吉姆

(因为它是载有一部分第一XML文件)

我的第二个XML文件有(基本):

 
                  吉姆

我想在ListView以某种方式消耗第二个文件以及,造成弗雷德被禁用。

我假定这将是某种形式的过滤器我会某处适用于XAML。

 < ListView控件名称=lvwSourceFiles
          保证金=11,93,0,12VerticalContentAlignment =中心
          的Horizo​​ntalAlignment =左WIDTH =306
          光标=手的TabIndex =6
          的ItemsSource ={结合}
          的SelectionMode =多
          的SelectionChanged =lvwSourceFiles_SelectionChanged>
    < ListBox.DataContext>
        < XmlDataProvider X:NAME =xmlSourceFiles的XPath =AssemblyUpdaterSource /来源/来源/文件/>
    < /ListBox.DataContext>
    < ListView.ItemContainerStyle>
        <风格的TargetType ={X:类型的ListViewItem}>
            < EventSetter事件=previewMouseRightButtonDown
                         处理程序=OnSourceListViewItem previewMouseRightButtonDown/>
        < /样式和GT;
    < /ListView.ItemContainerStyle>
< /&的ListView GT;


解决方案

这是一个相当复杂的任务,所以你应该考虑多在code,而不是在XAML中这样做。如果你在$ C $完全做到这一点的C-的背后,你可以添加一个处理程序ListView.Loaded事件,然后再做添加项目和禁止某些项目存在的所有逻辑。诚然,在ListView不会是数据绑定,但在这样一个特殊的情况下,你可能会更好没有约束力的。

不过,要证明这可以在XAML来完成,而用加价类似你,我已经构建了下面的例子。我的例子使用列表,而不是XmlDataProvider,但它的精神是完全一致的;你只需要更换我的code,它建立列出与加载XML你的code。

下面是我的code隐藏文件:

 公共部分类窗口2:窗口
{
    私人列表<&人GT; _persons =新的List<&人GT;();    公共窗口2()
    {
        的InitializeComponent();        _persons.Add(新的Person(乔));
        _persons.Add(新的Person(弗雷德));
        _persons.Add(新的Person(吉姆));
    }    公开名单<&人GT;人
    {
        {返回_persons; }
    }    公共静态列表<&人GT; FilterList
    {
        得到
        {
            返回新的List<&人GT;()
            {
                新的人(乔),
                新的人(吉姆)
            };
        }
    }
}公共类Person
{
    字符串_name;    公众人物(字符串名称)
    {
        _name =名称;
    }    公共字符串名称
    {
        {返回_name; }
        集合{_name =价值; }
    }    公共重写字符串的ToString()
    {
        返回_name;
    }
}

这只是定义了几列,而的类定义,它持有的名称的字符串。

接下来,我的XAML标记了:

 <窗​​口x:类=TestWpfApplication.Window2
的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
的xmlns:X =htt​​p://schemas.microsoft.com/winfx/2006/xaml
XMLNS:地方=CLR的命名空间:TestWpfApplication
的xmlns:SYS =CLR的命名空间:系统;装配= mscorlib程序
标题=窗口2HEIGHT =300WIDTH =300
的DataContext ={绑定的RelativeSource = {自我的RelativeSource}}>
< Window.Resources>
    <局部:PersonInListConverter X:键=personInListConverter/>
    < ObjectDataProvider的X:键=filterList对象实例={X:静态地方:Window2.FilterList}/>
< /Window.Resources>
<&StackPanel的GT;
    < ListView控件的ItemsSource ={结合人}
              的SelectionMode =多
              NAME =lvwSourceFiles光标=手VerticalContentAlignment =中心>
        < ListView.ItemContainerStyle>
            <风格的TargetType ={X:类型的ListViewItem}>
                < setter属性=IsEnabled
                        VALUE ={绑定名称,转换器= {StaticResource的personInListConverter},ConverterParameter = {StaticResource的filterList}}/>
            < /样式和GT;
        < /ListView.ItemContainerStyle>
    < /&的ListView GT;
< / StackPanel的>

下面我结合各自的ListViewItem到当前的人的姓名属性的IsEnabled属性。然后,我提供一个转换器,将检查,看看是否这个人的名字在名单。的ConverterParameter指向FilterList,这是您的第二XML文件的等效。最后,这里是转换器:

 公共类PersonInListConverter:的IValueConverter
{
    公共对象转换(对象的值,类型TARGETTYPE,对象参数,CultureInfo的文化)
    {
        字符串名称=(字符串)值;
        清单<&人GT;人=(参数作为的O​​bjectDataProvider).ObjectInstance的名单,LT;人取代;        返回persons.Exists(人=> name.Equals(person.Name));
    }    公共对象ConvertBack(对象的值,类型TARGETTYPE,对象参数,CultureInfo的文化)
    {
        抛出新NotImplementedException();
    }
}

和最终的结果:

OK, sorry for the overly broad question, but let's see what you guys suggest....

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

I have a second XML file with items that match what is in my ListView. However, if there is not a match in the 2nd file, then I want that ListItem disabled.

A simple example:

My ListView has in it:

                   Joe
                   Fred
                   Jim  

(because it was loaded with the first XML file)

My second XML file has (essentially):

                  Joe
                  Jim

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

I am assuming that it would be some sort of "Filter" 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>

解决方案

This is a fairly complex task, so you should consider doing this mostly in code rather than in XAML. If you were to do this entirely in code-behind, you could add a handler for the ListView.Loaded event, and then do all the logic of adding items and disabling certain items there. Admittedly, the ListView would not be data-bound, but in a special case like this you might be better off without the binding.

However, to show that this can be done in XAML, and using mark-up similar to yours, I have constructed the following example. My example uses Lists rather than XmlDataProvider, but the gist of it is exactly the same; you would just need to replace my code that builds Lists with your code that loads XML.

Here is my code-behind file:

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

This simply defines a couple lists, and the Person class definition, which holds a Name string.

Next, my XAML mark-up:

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

Here I bind the IsEnabled property of each ListViewItem to the Name property of the current Person. I then supply a Converter that will check to see if that Person's Name is on the List. The ConverterParameter points to the FilterList, which is the equivalent of your second XML file. Finally, here is the converter:

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();
    }
}

And the end result:

这篇关于如何禁用XAML一些项目的ListView的WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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