ListView复选框可在同一行中显示或隐藏文本框

时间:2015-05-20 11:34:53

标签: c# wpf listview mvvm checkbox

我有一个列表视图,列有复选框,其他列有文本框,列表视图中有多行。 我想根据选中和未选中的复选框设置文本框的可见和隐藏属性。 它是一个使用MVVM模式和PRISM的WPF项目。 请帮忙,我被困住了。

<ListView Width="Auto" ItemsSource="{Binding Path=PayFeeDetails}">
  <ListView.View>
    <GridViewColumn Header="Description" Width="110" >
      <GridViewColumn.CellTemplate>
        <DataTemplate>
          <Grid>
             <Grid.ColumnDefinitions>
               <ColumnDefinition Width="80"/>
               <ColumnDefinition Width="auto"/>
             </Grid.ColumnDefinitions>
             <TextBlock Grid.Column="0" Text="{Binding Path=Description}"/>
             <CheckBox Grid.Column="1" Width="30" Name="CommentCheckBox"/>
          </Grid>
        </DataTemplate>
      </GridViewColumn.CellTemplate>
    </GridViewColumn>
    <GridViewColumn Width="140">
      <GridViewColumnHeader Tag="GameName" Content="Game Name" />
       <GridViewColumn.CellTemplate>
        <DataTemplate>
          <Grid>
             <TextBox Text="{Binding Path=Balance}"/>
             <TextBlock Text="{Binding Path=Balance}"/>
          </Grid>
        </DataTemplate>
      </GridViewColumn.CellTemplate>
    </GridViewColumn>
   </GridView>
  </ListView.View>
 </ListView>

2 个答案:

答案 0 :(得分:0)

您需要为第二个Gridview网格的可见性添加绑定。 现在,在Gridview1上,itemclick创建一个INotify属性更改,并从第二个网格中设置为源的集合中更新该特定项目的Grid Visibility。

答案 1 :(得分:0)

我会走这条路

1)创建一个带有属性的对象,让我知道我可以编辑。

public class  ItemToBindTo:INotifyPropertyChanged
{
    #region Fields
    public event PropertyChangedEventHandler PropertyChanged;
    private bool _canEdit;
    private string _description;
    private string _balance;
    #endregion

    #region Properties
    public bool CanEdit
    {
        get
        {
            return _canEdit;
        }
        set
        {
            _canEdit = value;
               OnPropertyChanged("CanEdit");
        }
    }

    public string Description
    {
        get
        {
            return _description;
        }
        set
        {
            _description = value;
               OnPropertyChanged("Description");
        }
    }

    public string Balance
    {
        get
        {
            return _balance;
        }
        set
        {
            _balance = value;
               OnPropertyChanged("Balance");
        }
    }
    #endregion 
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
} 

2)我将为可见性创建转换器

public class BoolToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var val = value as bool? ;
        return val.HasValue && val.Value ? Visibility.Visible : Visibility.Collapsed;
    }

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

public class InvertedBoolToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var val = value as bool?;
        return !(val.HasValue && val.Value) ? Visibility.Visible : Visibility.Collapsed;
    }

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

我将转换器作为静态资源

     <sigColor:InvertedBoolToVisibilityConverter x:Key="InvertedBoolToVisibilityConverter"/>
    <sigColor:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter"/>

这将是你的新xaml

<ListView Width="Auto" ItemsSource="{Binding Path=PayFeeDetails}">
        <ListView.View>
            <GridView>
            <GridViewColumn Header="Description" Width="110" >
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="80"/>
                                <ColumnDefinition Width="auto"/>
                            </Grid.ColumnDefinitions>
                            <TextBlock Grid.Column="0" Text="{Binding Path=Description}"/>
                            <CheckBox Grid.Column="1" Width="30" Name="CommentCheckBox" IsChecked="{Binding CanEdit, Mode=TwoWay}"/>
                        </Grid>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn >
                <GridViewColumnHeader Tag="GameName" Content="Game Name" />
            <GridViewColumn.CellTemplate>
                <DataTemplate>
                    <Grid>
                        <TextBox Text="{Binding Path=Balance}" Visibility="{Binding CanEdit, Converter={StaticResource BoolToVisibilityConverter}}"/>
                                <TextBlock Text="{Binding Path=Balance}" Visibility="{Binding CanEdit, Converter={StaticResource InvertedBoolToVisibilityConverter}}"/>
                    </Grid>
                </DataTemplate>
            </GridViewColumn.CellTemplate>
            </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>