0

我试图弄清楚如何修改嵌入在 ListView/GridView 中的复选框的状态。问题是该复选框取决于单独 DataGrid 中某些记录的批准状态。(例如,我使用复选框作为批准状态 True = 全部批准,False = 无,null = 部分批准)。

由于 DataContext 是一个实体,它没有我可以用来处理它的布尔值。

<ListView x:Name="EmployeeNameListBox" Height="330" ItemsSource="{Binding}" SelectedValuePath="ID" SelectionChanged="EmployeeNameListBoxSelectionChanged">
    <ListView.View>
        <GridView AllowsColumnReorder="False" >                           
            <GridViewColumn>
        <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox IsThreeState="True" Checked="EmployeeCheckBoxChecked" Unchecked="EmployeeCheckBoxChecked" x:Name="CheckBoxHero"/>                                                  
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn>
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock>
                            <TextBlock.Text>
                                <MultiBinding StringFormat="{}{0}, {1} - {2}">
                                    <Binding Path="LastName" />
                                    <Binding Path="FirstName" />
                                    <Binding Path="EmployeeNumber" />
                                </MultiBinding>
                            </TextBlock.Text>
                        </TextBlock>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

这是我尝试更新审批状态的地方:

foreach (var employee in this.employees)
{
    var records = from a in this.dailyActivities where a.Employee == employee select a;

    var approvedRecords = from r in records where r.IsApproved == true select r;

    if (approvedRecords.Count() == 0)
    {
        // None Approved checkbox state = false
    }
    else if (approvedRecords.Count() == records.Count())
    {
         // All Approved checkbox state = true
    }
    else
    {
         // Some Approved Set Checkbox state to null
    }                
}
4

1 回答 1

0

在您的 ViewModel 类中创建一个Nullable bool (bool?)包装器属性(我假设您使用的是 MVVM 模式)并使用RelativeSource.

将您的逻辑放入getter该属性中-

public bool? IsRecordApproved
{
  get
  {
     .....
     // Your logic here
  }
}

每当您希望在 UI 上更新属性时,请在您的属性上引发属性更改事件。(INotifyProeprtyChanged事件应该由您的 ViewModel 类实现)

于 2012-09-05T17:12:37.283 回答