I have a listview with a column have checkbox and other column having textbox, with multiple row in the listview. I want to set visible and hide property of textbox depending on checkbox checked and unchecked. Its a WPF project using MVVM pattern and PRISM. Please help, I am stuck.
<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>
You would need to add a binding to the Visibility of second Gridview Grid. Now on Gridview1 itemclick create a INotify property change and update that particular item's Grid Visibility from the collection thats been set as source in second grid.
I would go this route
1) Create an object with a property that lets me know I can edit.
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) I will create converters for visibility
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();
}
}
I will have my converters as static resources
<sigColor:InvertedBoolToVisibilityConverter x:Key="InvertedBoolToVisibilityConverter"/>
<sigColor:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter"/>
And this will be your new 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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.