WPF - prevent ListBox item selection - wpf

I would like to prevent selection of ListBoxItems in my ListBox. My DataTemplate has a checkbox and this should be the only thing the user can click or select.
How can I do that?
Thanks!

This is almost a duplicate question. In fact, you're asking two questions here:
Either style your ListBoxItem so that it doesn't show selection (look elsewhere on SO for that answer), or replace ListBox with ItemsControl if you don't need the other features that ListBox provides.
Bind your checkbox's IsChecked property to the parent ListBoxItem.IsSelected property:
<CheckBox
IsChecked="{Binding
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType=ListBoxItem},
Path=IsSelected}"
/>

When your user will try to (un)check your checkboxes then item become 'active' in some way. And focused style will be applied. As far as i know there is no way to disable selection(because if you did your checkboxes will not work) but you can override focused(or selected) style of your listbox items

Related

listview checkbox binding wpf MVVM

I am trying to get the bool value of the checkbox present in the Listview. I am binding to a bool public property "Assignm" in the view model. I tried the below binding pattern but the problem is if i select one checkbox it selects all checkboxes and vice versa. I think this is because relativesource is listview and it works on complete listview. I also tried changing the relative source to ListviewItem but that didn't trigger anything. Can someone help me please. Do i need to change something here ?
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Tag="{Binding MU_Identifier}" IsChecked="{Binding DataContext.Assignm, RelativeSource={RelativeSource FindAncestor, AncestorType=ListView}}">
</CheckBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
Because your binding for IsChecked property is Assignm property which seems to be one property of your view model.
If there is a boolean property named Assignm for the data model of the DataSource of ListView, then just change the binding like this: {Binding Assignm}, as Tag property does.
All your items are bounded to a single property, so when one item changes a property in your context it changes on other items.
To provide correct work all your items from ItemsSource should have property IsChecked.
Check this Example

WPF bind a control visibility to the focused property of another control

I have a combobox that displays a list of items, and I want to place a button next to it which triggers a command to see the details of the selected item. So far, so good. Now I want the button to be visible only if the combobox has focus (or is in "edit" mode, but not only when the popup is open).
I thought I could bind the visibility of the button to some focus property of the combobox, something like this:
<Button Content="Details" Visibility="{Binding ElementName=elementListComboBox,
Path=IsFocused, Converter={StaticResource Bool2VisibilityConverter}}"/>
But I found no way to know if the control I want is focused or not. I looked at the FocusManager.FocusedElement, but I don't know how to get the focused control I want inside the binding. Is there a way to achieve this in XAML?
Ok, the way to get this working as I wanted is this:
<Button Command="{Binding SomeCommand}"
Content="Details"
Focusable="False"
Visibility="{Binding ElementName=elementListComboBox,
Path=IsKeyboardFocusWithin,
Converter={StaticResource Bool2VisibilityConverter}}"/>
Two key factors here: bind the button's visibility to IsKeyboardFocusWithin property of the combobox, and set the button's Focusable property to false, else it will get collapsed when you want to click on it.
Hope this is useful.

Disable button when datagrid is in edit mode

I have usercontrol with a DataGrid binded to an observablecollection of items and two buttons: save changes and discard changes.
My problem is that when the user is editing a datagridrow the buttons remain clickable but aren't executed.
Is there a way to disable the buttons when the DataGrid is in edit mode?
I tried this code with no success:
<Button Content="SaveChanges" Command="{Binding Path=CmdSaveChanges}"
IsEnabled="{Binding ElementName=MyDataGrid, Path=IsEditing, Converter={StaticResource InverseBooleanConverter}}" />
You are binding to an IsEditing property of an element named MyDataGrid which presumably is a DataGrid. However, DataGrid has no such property.
Although DataGridCell has an IsEditing property, there is no easy way to get the currently editing cell. DataGrid.CurrentCell will not give you the DataGridCell, but only a DataGridCellInfo.
You are perhaps better of with attaching handlers to the DataGrid's BeginningEdit and CellEditEnding events.

WPF - Not working IsReadOnly in DataGrid with custom CheckBox style

I currently have a custom CheckBox style which I use in a DataGrid. However, when I specify the IsReadOnly="true" property in a DataGridCheckBoxColumn, this is no longer working (can I still click and change the value of the CheckBox). I also used Expression Blend to extract a copy of the original WPF style, and this one has the same problem. Only the original CheckBox style seems to be working, which I don't want.
I currently manage to solve it by creating a new style based on my normal one with the IsHitTestVisible and Focusable properties on false, but I want the IsReadOnly property from the DataGridCheckBoxColumn to work without this workaround.
How can I achieve this result?
May be it is your case: try to set somewhere in the style this xaml:
IsReadOnly="{TemplateBinding}"
or
IsReadOnly="{Binding IsReadOnly, RelativeSource={RelativeSource TemplatedParent}}"
Hope this help.
There is an "IsEnabled" on CheckBox (should be on all other controls, I think) can do the job.

How to set focus to a control in a TreeViewItem when selected

I have a TreeView in which the items are defined by HierarchicalDataTemplates. Each TreeViewItem that is created has some TextBoxes in it. When a TreeViewItem is selected I want to set the Keyboard Focus to a TextBox of the TreeViewItem (the TextBoxhas the name TextBox1). How can I do this?
There are many ways you can do this. Here is just one of them. Use mine FocusExtension. IsFocused attached property. Bind it to TreeViewItem.IsSelectedProperty if you don't have a ViewModel underneath. Something like
<TextBox local:FocusExtension.IsFocused="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TreeViewItem}}}" />
should work. I typed that from head and didn't check the syntax. Be careful while copy-pasting :).

Resources