<my:DataGrid.RowHeaderTemplate>
<DataTemplate>
<Grid>
<CheckBox Name="dgChkSelect" Checked="dgChkSelect_Checked" Unchecked="dgChkSelect_Unchecked" Tag="{Binding}" />
<!--IsChecked="{Binding Path=IsSelected,Mode=TwoWay,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type my:DataGridRow}}}"-->
</Grid>
</DataTemplate>
</my:DataGrid.RowHeaderTemplate>
Hi all, I want to loop through the datagrid data to retrieve those records has been checked.
Anyone know how to do this?
You were in the right path add IsChecked="{Binding Path=IsSelected, Mode=TwoWay}". Dont have the rest of the relative source stuff. Datagrid's itemsource will be searched for the property IsSelected; Make sure you have a property. On a separate note why aren't you using
DataGridCheckBoxColumn.
Related
I've searched and tried many different things and can't find anything that works. I have two columns in a GridView that are CheckBoxes. The rows are of values that can be set to test the device connected.
Column one is saying if the row contains the default value and is bound to the value from the database whether or not it's checked. This can't be changed by the user. I've set the IsEnabled="false".
Column two allows the user to select a custom value other than the default value. I would love to have it so the row with the default value doesn't have this checkbox. Can't figure out how to make it invisible. So I bind the IsEnabled to a bool in the ViewModel that is set in the code behind based on if it's a default value or not before adding it to the ObservableCollection used for the ListView that holds this GridView. Yet the user can still click on this checkbox in the Default Value's row and it will change to show it was checked. I don't want that either.
I remember in WinForms there was a way to make the CheckBox completely ReadOnly but I could still manipulate its checked state in code. I can't seem to do that with WPF. Help!
XAML:
<GridViewColumn x:Name="lsvThresholdDefaultValue" Header="Default" Width="50">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox x:Name="cbxDefaultValue"
IsChecked="{Binding DefaultValue}"
IsEnabled="False"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn x:Name="lsvThresholdCustomValue" Header="Use This" Width="55">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox x:Name="cbxUseThis"
IsChecked="{Binding UseThisValue}"
IsHitTestVisible="False"
IsEnabled="{Binding AllowUse}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
Code Behind:
if (oneThreshold.DefaultValue)
{
oneThreshold.AllowUse = false;
}
else
{
oneThreshold.AllowUse = true;
}
EDIT:
Here is what the answer is that I got from my coworker (which is what I really wanted to do anyway).
At the top added (maintenance is my namespace):
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
<maintenance:InverseBooleanToVisibilityConverter
x:Key="InverseBooleanToVisibilityConverter" />
</Window.Resources>
Then on the checkbox, I replaced IsEnabled with Visibility:
<CheckBox x:Name="cbxUseThis"
IsChecked="{Binding UseThisValue}" IsHitTestVisible="False"
Visibility="{Binding AllowUse, Converter={StaticResource
BooleanToVisibilityConverter}}" />
Then to make the box NOT appear on the default value row, I added Visibility:
<CheckBox x:Name="cbxDefaultValue"
IsChecked="{Binding DefaultValue}" IsEnabled="False"
Visibility="{Binding AllowUse, Converter={StaticResource
InverseBooleanToVisibilityConverter}}"/>
The Inverse... was some code he wrote since there is no negate on BooleanToVisibilityConverter.
To display items on Telerik:RadGridView usually I use DataContext="{Binding [someViewModel]}" and ItemSource="{Binding objectList, Mode=TwoWay}".
and for my column I'll access the objectfield. The overall picture will be something like below:
<telerik:RadGridView DataContext="{Binding [someViewModel]}"
ItemSource="{Binding objectList, Mode=TwoWay}">
<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn>
<telerik:GridViewDataColumn.CellTemplate>
<DataTemplate>
<CheckBox IsEnabled="{Binding enabledVar}"
IsChecked="{Binding isChecked, Mode=Twoway}"
</DataTemplate>
</telerik:GridViewDataColumn.CellTemplate>
</telerik:GridViewDataColumn>
</telerik:RadGridView.Columns>
</telerik:RadGridView>
Imagine there will be 10 items in objectList. Each of the item in objectList will have a variable isChecked which to manipulate the checkbox IsChecked property.
I have another variable in the same viewmodel named enabledVar which to control the ten checkbox IsEnabled property. enabledVar is not part of the objectList but I couldn't get the value. May I know how to handle such case?
Updates:
I've found some new direction but not sure if it helps.
<CheckBox IsEnabled="{Binding enabledVar,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType=telerik:RadGridView}}"
but then of course, still failed.
Any helps would be very much appreciated.
if using Ancestor you have to bind to DataContext
<CheckBox IsEnabled="{Binding Path=DataContext.enabledVar,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType=telerik:RadGridView}}"
Another aproach could be to use element binding set name
<telerik:RadGridView x:Name="myGrid" ...
And then in the celltemplate bind to it
<CheckBox IsEnabled="{Binding Path=DataContext.enabledVar, ElementName=myGrid}"
Hope this helps.
The code suggested in Update will not work because GridViewDataColumn is not part of the Visual tree, and hence can not access DataContext.
You will have to make use of proxy data binding, like suggested here. You can get more examples by searching "wpf proxy databinding".
I have this combobox which contains a data tempate with a checkbox and a textbox. All is working but I would like to make it a little bit easier to select a value. Right now I have to click on the chechbox to change the value of the check box. Now I would like to be able to just click on the item in the combobox which also should toggle the checkbox.
Is this possible? If yes then how?
Here is a picture of my solution right now
Here is the code for my combobox
<ComboBox Name="employeeComboBox" Margin="2,0,2,0"
ScrollViewer.CanContentScroll="False"
DataContext="{Binding EmployeesOverviewViewModel, Source={StaticResource ViewModelLocator}}"
ItemsSource="{Binding Employees}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding Path=IsSelected}" Margin="2,0,2,2" VerticalAlignment="Center"/>
<TextBlock Text="{Binding Path=Name}" VerticalAlignment="Center"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Change your DataTemplate to this:
<DataTemplate>
<CheckBox IsChecked="{Binding Path=IsSelected}"
Margin="2,0,2,2"
Content="{Binding Path=Name}"
VerticalAlignment="Center"/>
</DataTemplate>
and it should work.
Why don't you use the Content property on the CheckBox?
<CheckBox Content="Hello, World" />
This way, the checkbox would toggle even when you click on the text (content).
As for your specific case, you can bind Name to Content instead of creating a separate TextBlock for it, and it should work as you want it to.
Whats happning here is, Your Checkbox and text are two different entities, you need to make them one, by simply using checkbox's text property and binding it. that way whenever you click on text your checkbox gets selected.
Is it possible to have multiple ItemSources for a single control?
Given the code below:
<ComboBox Margin="137,101,169,183" ItemsSource="{Binding collection}" SnapsToDevicePixels="True"
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox Command="{Binding CheckCommand}" IsChecked="{Binding IsChecked}" Content="{Binding Name}"/>
<TextBlock Text="" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
The TextBlock within the ComboBox DataTemplate requires data from another property within the VM than that of the ComboBox. How can this be achieved?
Thanks.
You can use RelativeSource-FindAncestor to reach up the visual tree and grab a different DataContext.
For example (assuming the command is what you want):
Command=”{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ComboBox}}, Path=CheckCommand}”
This should also serve as a good resource.
Edit: Typo and resources.
If i remember correctly, DataTemplates run within their own scope and cannot directly use ElementNames defined outside the DataTemplate. You could however get around it by using StaticResource and referring to that directly from TextBlock inside the template.
I haven't tried Ragepotatos's approach to go outside DataTemplate scope but would love to know if that works out for you too.
I have a listbox and I have set the itemstemplate as shown below.
XAML:
<ListBox ItemsSource="{Binding DataList}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox x:Name="CheckBox" HorizontalAlignment="Center" VerticalAlignment="Center" />
<TextBlock x:Name="TextBlock" Text="{Binding Title}" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10,0,0,10" FontSize="26.667" TextWrapping="Wrap"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I want to get which all check box has been selected.Is there any way to get checkbox control for each item so that I can check its IsChecked property.
I can think of a way of binding the IsChecked property.But Is there any other way to do it?
Yes. One way to do is to bind the IsChecked property. And if you are using MVVM, probably that's the right way to do it.
Anyways, if you don't want to go the binding way, and assuming you want to iterate over all items of listbox, and prepare a list of checked items, see if this helps:
WPF - Find a Control from DataTemplate in WPF
If you're already binding to the Title property in the item template then it would certainly make sense to bind to IsChecked, too.
If you really need to, you can walk the visual tree by using the VisualTreeHelper to find the CheckBox instances.
Binding the IsChecked property to a boolean property on your object instance contained within DataList would be the simplest and cleanest way. Alternatively, if you want to avoid code behind, then you could write an attached property.
See also How to access a specific item in a Listbox with DataTemplate?
I bet it cannot be simpler than that:
<ListBox SelectionMode="Multiple" >
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox
IsChecked="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}"
Content="{Binding Path=Content, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>