简体   繁体   English

如何将命令绑定到ListView WPF中的复选框?

[英]How to bind Command to Check Box in ListView WPF?

Update 更新

I edited the code below to match the suggestions and it works correctly now. 我编辑了以下代码以匹配建议,并且现在可以正常工作。

I've seen several stack overflow questions similar to this one, but I haven't quite been able to put it all together. 我已经看到了几个与此类似的堆栈溢出问题,但是我还不能完全将它们放在一起。 I have the following xaml code. 我有以下xaml代码。

<UserControl x:Class="AuditEfficiencyMVVM.View.AuditTestsMain"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:AuditEfficiencyMVVM.View"
         xmlns:viewmodel="clr-namespace:AuditEfficiencyMVVM.ViewModel"
         mc:Ignorable="d" 
         d:DesignHeight="500" d:DesignWidth="1000">

<UserControl.DataContext>
    <viewmodel:AuditTests/>
</UserControl.DataContext>

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>

    <Grid.RowDefinitions>
        <RowDefinition Height="50"/>
        <RowDefinition/>
        <RowDefinition Height="50"/>
    </Grid.RowDefinitions>

    <ListView Grid.Row="1" Grid.Column="0" Margin="10" ItemsSource="{Binding Path=Tests}">
        <ListView.View>
            <GridView>
                <GridViewColumn>
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox Name="TestSelected" IsChecked="{Binding Path=Selected, Mode=TwoWay}" Command="{Binding Path=TestSelected, RelativeSource={RelativeSource AncestorType=ListView}}"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="Test Type" DisplayMemberBinding="{Binding Type, Mode=OneWay}"/>
                <GridViewColumn Header="Progress">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <ProgressBar Name="TestProgress" Width="50" Height="20" Value="{Binding Progress, Mode=OneWay}"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="Status" DisplayMemberBinding="{Binding Status, Mode=OneWay}"/>                    
            </GridView>
        </ListView.View>

        <ListView.GroupStyle>
            <GroupStyle>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate>
                                    <Expander IsExpanded="True">
                                        <Expander.Header>
                                            <TextBlock FontWeight="Bold" FontSize="14" Text="{Binding Name}"/>
                                        </Expander.Header>
                                        <ItemsPresenter/>
                                    </Expander>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
        </ListView.GroupStyle>
    </ListView>

    <ListView Grid.Row="1" Grid.Column="1" Margin="10" ItemsSource="{Binding Path=Files}">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="File Type" DisplayMemberBinding="{Binding Type, Mode=OneWay}"/>
                <GridViewColumn Header="File Location" Width="250">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Text="{Binding Path=Location, Mode=TwoWay}" Width="225"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>                    
                <GridViewColumn>
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <Button Width="30" Height="20">...</Button>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>

    <Button Grid.Row="2" Grid.Column="1" HorizontalAlignment="Right" Margin="10" Width="50" Height="30">Run</Button>
</Grid>
</UserControl>

Here is my code behind 这是我的代码背后

public class AuditTests : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private RelayCommand _testSelected;

    private void AddTest()
    {
        MessageBox.Show("Success");
    }

    public RelayCommand TestSelected
    {
        get
        {
            return _testSelected;
        }
        private set
        {
            if (_testSelected != value)
            {
                _testSelected = value;
                RaisePropertyChanged("TestSelected");
            }                
        }
    }

    private void RaisePropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }

    public AuditTests()
    {
        TestSelected = new RelayCommand(AddTest);
    }

    public ObservableCollection<Model.File> Files
    {
        get;
        set;
    }

    public ObservableCollection<Model.Test> Tests
    {
        get;
        set;
    }

    public void LoadFiles()
    {
        ObservableCollection<Model.File> files = new ObservableCollection<Model.File>();

        foreach (Model.Test test in Tests)
        {
            foreach (Enums.FileType type in test.ExpectedSources)
            {
                Boolean containsType = false;
                foreach (Model.File file in files)
                {
                    if (file.Type == type)
                    {
                        containsType = true;
                        break;
                    }
                }

                if (!containsType)
                {
                    files.Add(new Model.File { Type = type, Location = "", Tests = new List<Enums.TestType> { test.Type } });
                }
                else
                {
                    files.Where(t => t.Type == type).First().Tests.Add(test.Type);
                }
            }
        }

        Files = files;
    }

    public void LoadTests()
    {
        ObservableCollection<Model.Test> tests = new ObservableCollection<Model.Test>();

        foreach (var prop in Enum.GetValues(typeof(Enums.TestType)).Cast<Enums.TestType>().ToList())
        {
            tests.Add(new Model.Test { Category = prop.GetCategory(), Type = prop, Progress = 0, Selected = true, Status = Enums.TestStatus.NotStarted, ExpectedSources = prop.GetExpectedFiles() });
        }

        Tests = tests;
    }        
}
}

From what I've read this seems like it should work, but when I check/uncheck the check box the message box is not activated. 从我阅读的内容来看,这似乎应该可行,但是当我选中/取消选中该复选框时,消息框未激活。 What am I missing here to get the check/uncheck command to work? 我在这里缺少什么才能使check / uncheck命令起作用?

TestSelected command is a property AuditTests object. TestSelected命令是属性AuditTests对象。 DataContext of CheckBox is Model.Test object. CheckBox的DataContext是Model.Test对象。 They are on different levels. 它们处于不同的级别。 You can bind command to a property from ListView DataContext with RelativeSource parameter: 您可以使用RelativeSource参数将命令绑定到ListView DataContext中的属性:

Command="{Binding Path=DataContext.TestSelected, 
                  RelativeSource={RelativeSource AncestorType=ListView}}"

First, I'd make AuditTests implement INotifyPropertyChanged , and raise PropertyChanged in all its property setters when their values change. 首先,我将使AuditTests实现INotifyPropertyChanged ,并在其值更改时在其所有属性设置器中引发PropertyChanged There's a lot of documentation out there on doing that. 有很多有关此操作的文档。

Second, you've got to give the user control a copy of it. 其次,您必须给用户控件一个副本。 I can't tell if you gave your UserControl any properties of its own that MainWindow may want to put bindings on; 我不能告诉您是否给UserControl提供了MainWindow可能希望绑定的任何属性; if that's the case, it's a more complicated question. 如果是这样,那就是一个更复杂的问题。 But the simplest thing is this: 但是最简单的事情是:

public AuditTestsMain()
{
    InitializeComponent();

    //  Now its parent view can't bind this guy's properties to properties of the 
    //  parent's view's viewmodel, because we've broken DataContext inheritance. 
    DataContext = new ViewModels.AuditTests();
}

Then look at ASh's answer for how to bind the command. 然后查看ASh的答案以了解如何绑定命令。 That's another DataContext thing: The thing there is that the listview items are Model.Test instances. 这是DataContext的另一件事:事情是listview项是Model.Test实例。 So inside that DataTemplate for the items, bindings bind to the properties of Model.Test by default, because that's the DataContext . 因此,在该项目的DataTemplate内部,绑定默认情况下绑定到Model.Test的属性,因为这是DataContext But the command is not a property of Model.Test ; 但是该命令不是Model.Test的属性; it's a property of AuditTests . 它是AuditTests的属性。 AuditTests is the UserControl's DataContext, and therefore it's the ListView's DataContext as well. AuditTests是UserControl的DataContext,因此也是ListView的DataContext。 Controls inherit the parent's DataContext unless something interferes with that -- like the line I'm adding to your AuditTestsMain constructor, or like the way the ListView creates child items which have the listview's items as their DataContext. 控件会继承父级的DataContext,除非有一些干扰(例如我要添加到AuditTestsMain构造函数中的行),或者类似ListView创建将listview的项作为其DataContext的子项的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM