【问题标题】:How to bind Command to Check Box in ListView WPF?如何将命令绑定到 ListView WPF 中的复选框?
【发布时间】:2017-04-14 18:02:06
【问题描述】:

更新

我编辑了下面的代码以匹配建议,现在它可以正常工作了。

我见过几个类似于这个的堆栈溢出问题,但我还不能把它们放在一起。我有以下 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>

这是我的代码

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

从我读过的内容来看,这似乎应该可以工作,但是当我选中/取消选中该复选框时,消息框未激活。为了使检查/取消检查命令起作用,我在这里缺少什么?

【问题讨论】:

  • 那不是代码隐藏,那是模型或其他东西。如何将AuditTests 类与AuditTestsMain UserControl 连接起来?
  • @EdPlunkett 我想我现在不看它。我将列表视图连接到可观察集合,但看起来我没有将整个视图连接到AuditTests。那会是 DataContext 吗?
  • 是的。在我看来,AuditTests 想成为一个视图模型。它应该实现INotifyPropertyChanged,并且当您使用UserControl 时,它的DataContext 应该有一个AuditTests 的实例。这是除了 ASh 在下面指出的内容之外。
  • @EdPlunkett 如何正确设置数据上下文?我在&lt;UserControl&gt; 中有DataContext = "{}",但我不确定如何指定文件。 AuditTestsMain.xaml 在 View/ 中,而 AuditTests.cs 在 ViewModel/ 中
  • 这将取决于谁负责创建AuditTests。定义类的文件夹无关紧要。您如何使用AuditTestsMain?你只是想把它的一个实例扔到任何地方并让它做它的事情吗?或者其他一些视图模型是否拥有AuditTests 的副本,并且它想在自己的视图中显示它?

标签: c# wpf data-binding icommand relaycommand


【解决方案1】:

TestSelected 命令是一个属性AuditTests 对象。 CheckBox 的 DataContext 是 Model.Test 对象。他们处于不同的层次。您可以使用 RelativeSource 参数将命令绑定到 ListView DataContext 中的属性:

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

【讨论】:

    【解决方案2】:

    首先,我要让AuditTests 实现INotifyPropertyChanged,并在其所有属性设置器中提高PropertyChanged 的值更改时的值。有很多关于这样做的文档。

    其次,您必须为用户控件提供一份副本。我不知道您是否为您的 UserControl 提供了 MainWindow 可能想要绑定的任何属性;如果是这样,这是一个更复杂的问题。但最简单的是:

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

    然后看看ASh的回答如何绑定命令。那是另一个 DataContext 事情:列表视图项目是Model.Test 实例。所以在项目的DataTemplate 中,绑定默认绑定到Model.Test 的属性,因为那是DataContext。但该命令不是Model.Test 的属性;它是AuditTests 的属性。 AuditTests 是 UserControl 的 DataContext,因此它也是 ListView 的 DataContext。控件继承父级的 DataContext 除非有什么干扰 - 比如我添加到您的 AuditTestsMain 构造函数的行,或者类似于 ListView 创建子项的方式,这些子项将列表视图的项作为其 DataContext。

    【讨论】:

      猜你喜欢
      • 2012-03-06
      • 2014-08-23
      • 1970-01-01
      • 1970-01-01
      • 2014-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多