CheckBox set ReadOnly but still clickable - c#

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.

Related

Convert ComboBox selection event to Checkbox toggle

I have a ComboBox that has a check box and a text block inside it.
I would like that when an item in the ComboBox is selected, the state of CheckBox contained by the ComboBox item is toggled.
I have looked a fair bit here and elsewhere on the internet but I didn’t find anything specific to this.
Please help. Thanks.
I would try something like this:
<ComboBox ItemsSource="{Binding MySource}">
<ComboBox.ItemTemplate>
<DataTemplate>
<ToggleButton x:Name="Wrapper"> <!-- CheckBox are good too -->
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding TheString}" />
<CheckBox IsChecked="{Binding Path=IsChecked, ElementName=Wrapper, Mode=OneWay}" />
</StackPanel>
</ToggleButton>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
You just have to rewrite the template for the the "Wrapper" ToggleButton (or CheckBox) so that it only shows its content.
I hope I was helpful.

Using Datagrid HeaderTemplate button to change Datagrid Row buttons

I have a datagrid bound to a collection of timesheet lines.
In each row I have included a custom usercontrol consisting of 3 Radio buttons in the last column. These indicate whether each line should be "Bill", "Hold" or "Write off".
In the header template for the datagrid, i have included a slightly different control which allows the user to select "Bill All", "Hold All", "Write Off All"
[The buttons in question...1
How can i get the header buttons to affect change on the row buttons beneath.
This is my first attempt at using WPF, and I'm trying to ensure i stick with proper WPF ideals and structures. I have tried to assign an Click event to the header buttons which would create an array of the row buttons, setting their IsSelected property to true.
But i'm sure i should be using something like an Attached Property.
These are the Radio buttons wrapped up into a UserControl
LineButtons.xaml
<StackPanel Orientation="Horizontal" IsHitTestVisible="True">
<RadioButton Style="{StaticResource GridLineButton}" x:Name="BillButton" Template="{DynamicResource ToggleButtonLeft}">Bill</RadioButton>
<RadioButton Style="{StaticResource GridLineButton}" x:Name="HoldButton" Template="{DynamicResource ToggleButtonMid}">Hold</RadioButton>
<RadioButton Style="{StaticResource GridLineButton}" x:Name="WOButton" Template="{DynamicResource ToggleButtonRight}">Write Off</RadioButton>
</StackPanel>
HeaderButtons.xaml
<StackPanel Orientation="Horizontal" IsHitTestVisible="True">
<RadioButton Style="{StaticResource GridLineButton}" >Bill All</RadioButton>
<RadioButton Style="{StaticResource GridLineButton}" >Hold All</RadioButton>
<RadioButton Style="{StaticResource GridLineButton}" >Write Off All</RadioButton>
</StackPanel>
Datagrid.xaml
<DataGridTemplateColumn>
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<local:HeaderButtons Padding="0"/>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<local:LineButtons Padding="4"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
I want all the row buttons to be setable from the header buttons.
E.g. If a user clicks Bill All (in the header) i'd like all the row "Bill" buttons to be set to Selected.
Any help would be much appreciated.

Combobox selections

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.

How to read the checkbox value in RowHeaderTemplate?

<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.

How can I modify an instance of the button defined in a Cell Header?

I have the following XAML class:
<ListView Controls:ListViewColumns.Stretch="true"
Name="myListItems" SelectionMode="Single">
<ListView.View>
<GridView>
<GridViewColumn Width="50">
<GridViewColumn.Header>
</GridViewColumn.Header>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Name="btnDelete"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
Padding="0"
Margin="0"
IsEnabled="{Binding Converter={StaticResource inverseBoolConverter},
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type WO:OpCompTab}}, Path=DisableAll}"
Click="btnDelete_Click" Tag="{Binding Path=.}">
<Image Source="/Win7;component/Images/Icons/Remove.png"
Width="{StaticResource IconWidth}"
Height="{StaticResource IconHeight}" />
</Button>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
The listview gets populated by hooking an observable collection up to its ItemSource property. This generates a neat looking column of Delete buttons for each item in the observable collection.
But this presents a quandary: How do I access the "btnDelete" of an individual row of the ListView? I'd like to change its icon on a per-row basis. Is it just a matter of binding the observable collection to the cell template somehow?
Instead of hardcoding the Image.Source, bind it to a property in your row object. Another way is to create a value converter and pass in the row or some property if you can't or don't want to modify the row object to add an ImageSource property. Have the value converter return the correct image source based on the passed in property or some other logic.

Categories

Resources