BitmapFrame in WPF ComboBox - wpf

I have a dialog that lets the user pick a custom icon. Loaded icons are displayed in a combobox in which the user makes the selection. Here is the XAML code for the combobox:
<ComboBox
Style="{DynamicResource IconComboBox}"
ItemTemplate="{StaticResource IconTemplate}"
ItemsSource="{Binding Icons,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type UserControl}}}"
SelectedItem="{Binding Icon}"
/>
Icons is a property that returns an array of BitmapFrame.
Icon is a property for the currently selected.
What currently doesnt work is that when I through the code behind set the Icon property (for example, when displaying which Icon the user picked the last time the dialog was shown), the Combobox selection is just blank. As if I had set the property to null before calling OnPropertyChanged.
My current theory is that WPF cannot compare BitmapFrames, so when walking through the list it fails to determine any of the icons there the "equal" and thus assumes null.
In short; what is the best way to through code behind set the currently selected item, which is a BitmapFrame, in a combobox?

You can use SelectedItem or SelectedIndex in the code behind to set the currently selected item!

Related

How to load Combobox-items on dropdown

I need to work with an excel workbook, with some worsheets.
To load the available worksheets into a combobox, I need to open the workbook and iterate through all sheets and add them to the combobox. This cost a lot of time, the user needs to wait for. If the user doesn't want to change the preloaded property, this time is waisted. So I only want to fill in the listitems when needed but show the preselected property, that was loaded from a settingsfile on startup of the app.
So is there a way to load the items on dropdown? Or better, how can I bind a command that is triggerd on dropdown, to load the items?
<ComboBox SelectedItem="{Binding Worksheet}" ItemsSource="{Binding WorkSheets}" x:Name="cbWorkSheet" Padding="4">
... oh, I know how to bind a command to a button so I need a special tip for the combobox, not for commands in general :)
UPDATE
I solved to trigger the "loading" and the List is filled but I need to show the last selected value (that is loaded on startup as the bound property Worksheet)
<ComboBox
x:Name="cbWorkSheet"
SelectedItem="{Binding Worksheet}"
ItemsSource="{Binding WorkSheets}"
IsEditable="True"
Padding="4">
<i:Interaction.Triggers>
<i:EventTrigger EventName="DropDownOpened" SourceObject="{Binding ElementName=cbWorkSheet}">
<i:InvokeCommandAction Command="{Binding LoadBACnetWsCombobox}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
With that Binding, the Combobox remains empty till I dropdown it :(
Original Answer: Incorrect, Posted 2020/06/02:
If you need SelectedItem populated before you open the dropdown, then you should probably change your EventTrigger's EventName from DropDownOpened to be Loaded.
That way, before the combobox is shown to the user, LoadBACnetWsComboBox will run, and hopefully your Worksheet will be set. Then, the combobox will be shown to the user.
Modified Answer, Posted 2020/06/03:
If you want to just populate the initial displayed text, you can always set the Text property of the combobox. It will get overridden once you do selections from the drop down.
<ComboBox
...
Text="{Binding MyInitialValue}">

Combobox: Get text and selected item in mvvm way

The combobox is editable so user can also write. I have two usecases:
Get the text from combobox in a Lostfocus way, when user writes
something in the box and when he presses "Tab" then I want the text
from the combobox and I add the value in the itemsSource list.
When the users makes the selection from the combobox dropdown, I want that
selected item as soon he selects it and this time I dont
want to have it in Lostfocus manner but somewhat like
PropertyChanged way.
I tried the code which is given below:
<ComboBox Margin="3" x:Name="Combobox" SelectedItem="{Binding SelectedPath, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Text="{Binding PathLocation, UpdateSourceTrigger=LostFocus, ValidatesOnNotifyDataErrors=True}" IsTextSearchEnabled="True" VerticalContentAlignment="Center" ItemsSource="{Binding SelectedPaths}" IsEditable="True" Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="2" HorizontalAlignment="Stretch"/>
Things worked fine for the first time when the application starts but after some interactions the problem arises. When the user starts typing in the combobox the SelectedItem property of combobox triggers which is contrary to what I want in the first use case.
In short: when the user writes something in the combobox I want to have it in a Lostfocus manner and when he makes the selection from the dropdown of combobox I want to have it in a PropertyChanged manner.
Let me know if more details are required.
I removed the "IsTextSearchEnabled" property but it also didnt work then I came to know that "IsTextSearchEnabled" property of Comobobox is by default true, which is causing some values suggested by the combobox are setting in my properties. As soon as I made the "IsTextSearchEnabled" to false, it is working fine.

Trying to get the currently selected item from a ComboBox DataTemplate inside a DataGrid off an DropDownClosed Event Handler

I previously had a ListView that was displaying an ObservableCollection of "Player" object properties, which I'm trying to convert into a DataGrid. I have most of it working, but currently having some issues with seeing changes on one particular property (Status), which is represented by a ComboBox. The idea is to allow players to override the "Status" value between a set of enums representing things like "Alive, Dead, Poisoned," etc. I've hooked up an EventHandler for when the ComboBox is closed and inside that handler, try to grab the sender object as a Player so I can send out the valid player values.
Here's a snippet of the XAML where I'm creating the ComboBox via a DataTemplate.
<DataGridTemplateColumn Header="Status">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox Name="cbStatus"
ItemsSource={Binding Source={StaticResource statusTypes}}"
SelecteItem="{Binding statusType, Mode=TwoWay}"
DropDownClosed="cbStatusType_DropDownClosed"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
Inside the Event Handler, I'm using the following to try and grab an "Player" object based on the values coming back from that particular row of the GUI.
Player playerOverridden = (Player)(sender as FrameworkElement).DataContext;
However, when I'm debugging the new playerOverridden when the ComboBox closes and a new value is selected, I'm not seeing that value being captured in playerOverridden.
This is pretty much the exactly what I was doing in a ListView with GridViewColumn.CellTemplates and it was working just fine. Not sure why the Status value is coming back as whatever it was initially set to instead of what the player has selected from the ComboBox.
Had to set the UpdateSourceTrigger on the SelectedItemBinding to be PropertyChanged, it works. Not sure why this has to be set explicitly inside a DataGrid where it's not something I needed to do in the ListView.
SelectedItem={"Binding statusType, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

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.

How to trigger PreparingCellForEdit event from a (Edit)button click in Silver light 4

I have a Data Grid in Silverlight 4 with 3 columns along with a column which contains "Edit/Apply" button.
The row cells are initially rendered as plain text and I need them to be changed to Comboboxes in the edit mode.
Once the Edit button in any of the row is clicked. I need to change the textblock( This is my Cell Template) in one of the row to the ComboBox(This is my Cell Editing template)
The question is how do i facilitate this on clicking the Edit button of each row and not by double clicking on the row.
Thanks,
Vijay
1st way
Put the textblocks on top of the combo-boxes (comboboxes with collapsed visibility). On Edit Switch visibilities between controls (Combo - visible / TextBlock - Collapsed) and Bind the Text Property from the Textblock to the selected value from the combo.
2nd way
Put only combo-boxes with IsReadOnly Property set to True. On Edit set IsReadOnly to false and on save set it back to true.*
3rd way
Make the datagrid readonly and bind a Data Form to it. The Data Form contains edit / save / cancel buttons.
If you need an example just let me know and I'll write one as soon as possible.
Not sure if this is what you expected. If not, please just ignore it. It is possible that I missunderstood the question.
Another answer
The other answer will be to use a DelegateCommand binded on the Command property of the Edit button wich can contain a parameter (the row number). This is if you are using the MVVM pattern. And in the ViewModel you could edit the selected row.
After a bit of searching / trying i was able to toggle between display and edit mode by a button click (button placed in each row).
Below posted is the sample code , which facilitates this toggle for one of the Cells in the Grid, Which makes use of Two Boolean Properties ShowDefaultTemplate and ShowEditableTemplate , The VisibilityConverter converts the boolean values to corresponding Visibility Options (Visible or Collapsed).
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding XXX}" HorizontalAlignment="Center" VerticalAlignment="Center"
Visibility="{Binding ShowDefaultTemplate, Converter={StaticResource visibilityConverter}}" />
<ComboBox HorizontalAlignment="Left" MinHeight="24" Width="100"
ItemsSource="{Binding Source, Source={StaticResource Provider}}"
Visibility="{Binding ShowEditableTemplate , Converter={StaticResource visibilityConverter}}"
SelectedItem = "{Binding SelctedItem,Mode=TwoWay}" />
</StackPanel>
</DataTemplate>
Thanks,
Vijay

Resources