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

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.

Related

WPF ComboBox is not collapsing dropdown once it loses focus

I have a very strange situation. I have ComboBox which is loaded with some items. When I click on it, the drop-down expands showing all items.
If I now open another application such as Notepad, the dropdown will not collapse which I would expect to happen. As a result, any overlapping application, such as opened Notepad, will appear as being located in between the WPF hosting the ComboBox and the ComboBox drop-down like on screenshot below. Probably the z-order is somehow totally wrong.
I believe the reason for this behavior is the fact that the dropdown did not collapse at the first place. My ComboBox is part of a Usercontrol and its xaml is like:
<ComboBox ItemsSource="{Binding ValueSet}"
SelectedItem="{Binding SelectedNode}"
DisplayMemberPath="Name"
BorderBrush="Green"
BorderThickness="1" SelectionChanged="ComboBox_SelectionChanged">
</ComboBox>
In my opinion there is no any problem with the z-order in your xaml. The actual problem is in the ComboBox template in your application. You should find the actual style(or control template) your Combo is based on and check the Popup(usually named "PART_Popup") control inside that control template. The IsOpen property of the Popup should be bound to the ComboBox IsDropDownOpened property, like that:
`IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}"`
Any way you should search the solution for your problem in incorrect control template of the Combo control. There is something that make it be opened(probably by making the IsOpen property of the inner Popup control to be always true). Let me know if it you need more suggestion.

Disable extra row in listview wpf

I have this ListView and at the end of the last item you can click to the right and type in a TextBox, like shown. How can I disable this?
If you want to prohibit writing in TextBox of your DataTemplate of ListView, then just set 'IsReadOnly' property of TextBox to true. Let me show an example:
<TextBox IsReadOnly="True" />
Or just use TextBlock instead of TextBox. TextBlock, by default, does not give an opportunity to write inside this control:
<TextBlock Text="I am a TextBlock. You cannot change this text.:)"/>

Have two controls set the visibility of another control

Sorry for the title, I just don't know how to explain it in one sentence.
So here is my goal: I need to have a boolean in my ViewModel define the visibility for a control (border).
I know I can achieve this with a BooleanToVisibilityConverter, but there is a little more to it. I want a button on my UI to be shown if the control is not visible. Once that button is pushed, then I want the boolean in my ViewModel to be TRUE and then I want the control to be visible and the button that was just pushed to be collapsed. Once that control is visible, I would like a button within that recently visible control to make the control collapsed and then make the original button visible.
Basically, there are two buttons: 1 to make visible (then collapse itself) and the other is to collapse its container and then make the first button visible.
I am trying to do all this with MVVM so if I can avoid code behind in my View that would be ideal!
Since you're using ICommands on your viewmodel, this should work...Assume your commands are "ShowBorderCommand" and "HideBorderCommand" and the property on your viewmodel is "ShowBorder"
<ConverterNamespace:BooleanToVisibilityConverter x:Key="BoolToVis"/>
<ConverterNamespace:ReverseBooleanToVisibilityConverter x:Key="BoolToCollapse"/>
<Border Visibility="{Binding ShowBorder, Converter={StaticResource BoolToVis}}">
<Button Command="{Binding HideBorderCommand}"/>
</Border>
<Button Command="{Binding ShowBorderCommand}" Visbility="{Binding ShowBorder, Converter={StaticResource BoolToCollapse}}"/>
My WPF Converters library has a BooleanToVisibilityConverter that allows reverse conversions, as well as allowing the use of Hidden instead of Collapsed:
<con:BooleanToVisibilityConverter x:Key="ReverseBooleanToVisibilityConverter" IsReversed="True"/>
<Button Visibility="{Binding SomeProperty, Converter={StaticResource ReverseBooleanToVisibilityConverter}}"/>

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 - prevent ListBox item selection

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

Resources