“列表视图”复选框以显示或隐藏同一行中的文本框
本文关键字:一行 文本 隐藏 列表视图 视图 列表 复选框 显示 | 更新日期: 2023-09-27 17:49:52
我有一个列表视图与列有复选框和其他列有文本框,在列表视图多行。我想设置可见和隐藏属性的文本框取决于复选框选中和未选中。这是一个使用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>
你需要给第二个Gridview的Visibility添加一个绑定。现在在Gridview1 itemclick上创建一个INotify属性更改,并从第二个网格中设置为源的集合中更新特定项的Grid Visibility
我会走这条路
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>