Checkbox in gridviewcolumn(header) - c#

I am using a listview (gridview/gridviewcolumn)
where the first column
contains only checkboxes for each row. Instead of adding a select all button I want
to add a Checkbox into the header of the first column.
Selecting the checkbox in the header will select
all other checkboxes and vice versa.
How can I do that in xaml?
Update:
This is the important part of my xaml code. (simplified)
<ListView ItemSource="...">
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsSelected}" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>

To have a checkbox on top of GridViewColumn you can do something like this
<GridViewColumn>
<GridViewColumn.Header>
<CheckBox/>
</GridViewColumn.Header>
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsSelected}" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
Now you can handle the check event and in the code you can iterate through your itemsSource and change values or if you are following MVVM you can bind property with checkbox so that whenever check is changed you will be notified through INotifyPropertyChanged. Once you find out through binding that check has changed, again you can change your itemssource accordingly

Related

How to access checkboxes in a List view one by one

I want to check all text boxes on the basis of if the check box is checked. But I don't know how to access all check boxes one by one? I bound it with ApprovalStatus which is of boolean type. Can any one help me to have code in C#?
<CheckBox Content="Check All" Height="16" HorizontalAlignment="Left" Margin="9,193,0,0" Name="Tab2CheckAll" VerticalAlignment="Top" Width="77" Click="Tab2CheckAll_Click"/>
<ListView Height="213" HorizontalAlignment="Left" Margin="9,215,0,0" Name="Tab2EmployeeEffortList" VerticalAlignment="Top" Width="771" AllowDrop="True" IsTextSearchEnabled="True">
<ListView.View>
<GridView>
<GridViewColumn Header="Approved" Width="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox CommandParameter="{Binding}" IsChecked="{Binding ApprovalStatus}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
You need not to access individual checkBoxes. CheckBox is already bounded to property set that instead.
Loop over ItemsSource of ListView and set ApprovalStatus to true for all items in the collection. As long as your underlying source class is implementing INotifyPropertyChanged, it will work fine.

Within a ListView - GridViewColumn change content based on ComboBox Selection

I am struggling trying to understand how I can allow a user to select a value from a combo box in a GridView column and have that selection impact other items within that GridView column (Labels, Textbox, etc). If they simply select an item from the combo box I don't appear to have the selected item / index from the ListView nor do I have the GridViewColumn so I can access its contents...This is most likely user error as I am new to WPF. Any suggestions would be greatly appreciated.
So in the xaml below I want to be able (for example) change the Label content for a given combo box selection. I can detect the combo box selection in code behind, but need to be able to access the other elements in that grid view column.
EDIT: ListView could be the wrong choice to begin with. I tried DataGrid as well, but this seemed to allow more flexibility within the columns for dynamic content.
Xaml:
<Grid x:Name="GridWindows" DockPanel.Dock="Bottom">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ListView x:Name="LvValues" ItemContainerStyle="{StaticResource AlternatingListViewItemStyle}" AlternationCount="2">
<ListBox.Items>
<sys:String>Value1</sys:String>
<sys:String>Value2</sys:String>
<sys:String>Value3</sys:String>
</ListBox.Items>
<ListView.View >
<GridView>
<GridViewColumn Header="Value" Width="120" DisplayMemberBinding="{Binding}" />
<GridViewColumn Header="Terminal" Width="50">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="False"></CheckBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Distribution" Width="400" x:Name="Distribution">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<ComboBox Width="100" x:Name="CbDistribution" SelectedValuePath="Content" SelectionChanged="CbDistribution_OnSelectionChanged">
<ComboBoxItem x:Name="Beta">Beta</ComboBoxItem>
<ComboBoxItem x:Name="Constant">Constant</ComboBoxItem>
<ComboBoxItem x:Name="Gamma">Gamma</ComboBoxItem>
</ComboBox>
<Label x:Name="DistributionArg1" Content="k="></Label>
<Label x:Name="DistributionArg2" Content="Theta="></Label>
<Label x:Name="DistributionArg3" Content="Other="></Label>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>

How to enable/disable TextBox according to ComboBox in ListView

Is it possible to enable/disable TextBox according to ComboBox selected value (for example enable it if selected value is "From To"?
<ListView Height="120" HorizontalAlignment="Left" Margin="19,92,0,0"
VerticalAlignment="Top" Width="500"
SelectionMode="Multiple"
ItemsSource="{Binding Products}">
<ListView.View>
<GridView>
<!--another columns-->
<GridViewColumn Header="Selection Mode">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ComboBox Width="70" Name="SelectionMode">
<ComboBoxItem Content="From To" IsSelected="True" />
<ComboBoxItem Content="List" />
</ComboBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Width="70"></TextBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
I'd suggest that you use ComboBoxItem's values as opposed to content. You'll need to write a binding which will bind your combobox SelectedValue (see my suggestion) to Enabled of your textbox. The binding will use a converter (IValueConverter), which will return True or False depending on SelectedValue value passed in - based on your query if SelectedValue == 'FromTo', your converter will return True, otherwise False.
I'd also suggest that you use objects, backing your UI elments, which is by all means a cleaner way of doing things.

How to bind a Dictionary<enum,bool> property to a GridViewColumn?

I am trying to bind a Dictionary property stored in a EffectViewModel inside an ObservableCollection, but it appears as "(Collection)" in the column I want to see as checkboxes.
Xaml for the GridViewColumn is this:
<GridViewColumn
Width="100"
Header="GPU"
DisplayMemberBinding="{Binding ShaderSupport}">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Margin="0"
HorizontalAlignment="Center"
IsChecked="{Binding Value}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
ShaderSupport is of type Dictionary<ShaderType, bool> where I just want to read in the bool value for ShaderType.GPU for this GridViewColumn.
Any ideas?
EDIT: Using this shows me the bool value as string, so I am in the right path I think:
DisplayMemberBinding="{Binding ShaderSupport[GPU]}">
Try this:
<GridViewColumn
Width="100"
Header="GPU">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Margin="0"
HorizontalAlignment="Center"
IsChecked="{Binding ShaderSupport[CPU]}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>

Having trouble binding list/grid to collection

I have set up a grid and bound it to a collection. I can edit the items in the collection through my grid and the changes get propagated to the collection. And, the GUI is showing everything in the collection at the time the ItemSource is set. But, I am programmatically changing some of the items in the collection (after the ItemSource is set) and these changes aren't reflected in the grid/GUI. Is there something else I need to do in order to get it to refresh. FYI, for the fields I want to edit (MoveToResource, ResourceKey, and Resource Type), I have set the mode to TwoWay. Below is my grid.
<ListView Name="lstXAMLStrings" Margin="5" Grid.Row="1">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="Extract?">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Content="" IsChecked="{Binding Path=MoveToResource, Mode=TwoWay}" ></CheckBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Text">
<GridViewColumn.CellTemplate>
<DataTemplate>
<local:RichTextBlock RichText="{Binding Path=FormattedMatchedLines}" TextWrapping="Wrap" Width="650"></local:RichTextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Key Name">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=ResourceKey, Mode=TwoWay}" Width="150"></TextBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Resource Type">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Source={StaticResource odp}}" SelectedItem="{Binding Path=Resource, Mode=TwoWay}"></ComboBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
Does your [view]model class implement INotifyPropertyChanged and fire the event whenever the property set accessor is used?
You need to make sure that the collection itself that you're databinding to is an observable collection (a class that implements the INotifyCollectionChanged interface). You might be able to alternatively roll your own class that implements INotifyCollectionChanged, but that's the only reason ObservableCollection exists so it could save you some time.
There's an msdn article on how to do it.
You need to make sure that your collection items implement INotifyPropertyChanged.
If each item you're changing programatically (correctly) implements that, your ListView/GridView will stay current.
This will work if you modify your collection items programatically, or in another screen.

Categories