Is there a way to find out which radio button is checked in WPF, similar to how this is done in regular forms code:
Control rb = gbTop.Controls.OfType<RadioButton>().FirstOrDefault(r => r.Checked);
I have a lot of radio buttons, and would rather not have to check for each one with an "if".
The Groupbox control doesn't seem to have a Controls property. I don't see any other way to see what's in them.
I could use a different "grouping" container if that works better.
Thanks
I found it. Should have used a stackpanel, and then this code, a slight variation of the above.
Control rb = StackPnl.Children.OfType<RadioButton>().FirstOrDefault(r => r.IsChecked == true);
Related
I've been trying to create a toggle button with a "simple" bevel effect for my WPF Window. At first I've tried to apply the BevelBitmapEffect but apparently it is not deprecated and there are no visual changes when applied.
I've have tried many other "tweaks" with no success.
The kind of button I'm going for is something like this:
Edit: I was able to create this effect by having two pictures (unpressed and pressed). Still, if anyone knows how to do this without any external resources that would be great.
You have two options for this,
Create a Style for the ToggleButton that gives the desired look, this page has an example of a simple style for a toggle button.
Make a toggle button custom control, which is the same idea but can be a bit simpler if you need to use the same style in multiple places or applications.
I need a listview with multiple buttons for scrolling. E.g.
ScrollToTop Button
ScrollUp Button
ListView
ScrollDown Button
ScrollToBottom Button
I have got the buttons working in a WPF application by using the code mentioned here. Now, I need to reuse this by making it a control(lookless?). The layout of the listview and buttons can be horizontal or vertical. Should I use custom control or user control?
Here's what I'd recommend.
Don't use any pre-composed elements. Create a behavior ScrollList, accepting two parameters - Direction and Target, Direction will be Top || Bottom, Target you list - again you can use ElementName binding.
The reason why I'd recommend this approach is the actual code required to scroll your list is tiny, while managing layouts via properties in WPF is proved to be mess an anti pattern (yes you can go ControlTemplates, but it's definitely too havy for what you're trying to do).
If behaviors are too complex just consider creating a couple of commands.
I'm trying to achive displaying the RowDetailsTemplate of a Silverlight DataGrid depending on a bool Property, bound to a CheckBox Control's IsChecked Property. Insinde of my RowDetailsTemplate there is a single custom UserControl, containing further Controls.
Since the DataGrid only allows a global setting (RowDetailsVisibilityMode) Some code-behind is needed. I've implemented a solution based on Rorys Reply (and using a behaviour-technique) which actually works.
Unfortunately, The DataGrid doesn't remember the individually shown or hidden Rows on sorting. The checkbox remains selected, but the Row collapses. Further, no event like "OnAfterSort" or something similar seems to exist, where i could "Refresh" the visibility settings in a loop.
Another idea was to bind the Visibility of my custom Details-UserControl to the CheckBox bound value. This actually works (when settings RowDetailsVisibilityMode to "Visible"), but I'm not able to get rid of this weird behaviour: When the CheckBox is checked, the Detail Template expands and the detail UserControl appears. Nice. When the CheckBox is unchecked again, the UserControl disappears (Visibility is set to Collapsed) but the Row doesn't collapse and the blank space remains (as it would be set to Hidden not Collapsed).
Do you have any ideas?
I hope it's ok I didn't post any code samples, the implementation is pretty easy and I believe that the problem doesn't actually lie in a coding mistake i made. You can setup a simple DataGrid quickly like in this perfect MSDN Example. Starting from here, it's easy to test both described behaviours!
Really big thanks in advance,
- Thomas
We are using WPF and have a window derived from a DockingLibrary. This window has a grid with multiple items in it, one being a WPF datagrid. We are using the M-V-VM pattern. When this windown is created and shown, none of the rows in this datagrid are selected. We can set the row to display as highlighted by doing something like:
SharedWindow.ShipmentWin.shipmentDataGrid.SelectedIndex = 0;
This causes the first row in the datagrid to be shown as highlighted. But, and isn't there always one of these, this row is not Selected nor does it have Focus. I tried setting IsSelected and Focus on this row as in:
SharedWindow.ShipmentWin.ShipVM.IsSelected = true;
bool focused = SharedWindow.ShipmentWin.shipmentDataGrid.Focus();
Am I going about this all wrong and is there some other way to Select the first row in the datagrid and set focus to it? Typically, when a datagrid is created, no row is selected until the user mouse clicks on the desired row.
Any thoughts would be greatly appreciated.
thanks!
Have a look at the FocusManager. It allows you the set the focus to another UI element via the SetFocusedElement method. Additionally, it allows you to determine the currently focused element in your application which is can get handy to debug focus issues. Snoop can be useful, too. It shows the currently focused element in the bottom status bar.
If you use the DataGrid from the WPF Toolkit, be prepared to find some bugs in relation to focus handling. Some have been addressed recently.
It's also worth understanding the difference between logical focus and keyboard focus, which are quite different animals. The .Focus() method sometimes only sets logical focus - which probably isn't what you want.
The documentation for the Focus method tells you that it will return true if the keyboard focus was set, and false otherwise.
i am trying to create a wpf app and have different parts in user controls.
in the navigation i have some buttons (now using the ribbon ctp). is it possible to change the main user control when different buttons are pressed in xaml. or is this just a bad way to do things?
sorry, really new to xaml and im trying to get my head arround it.
Further to what Carlo has said,
The way we do it is to have a blank grid in the place you want your controls to all appear and then use BlankGrid.Children.Clear() and BlankGrid.Children.Add() to set up which control is visible in this position.
We found that was the nicest programatically as we have a large number of custom controls, but Carlo's method would work nicely if you wanted to use the designer.
I think this is a pretty regular procedure in WPF. In my experience, me and other programmers put the controls where we want to show them and make their visibility hidden, collapsed or visible depending on what we want to show the user.