How to change icon on a Telerik RadToggleButton in WinForms? - winforms

How do I add and change icons on a Telerik RadToggleButton in a WinForms application? I want to add something similar to the pushpins used on Telerik's documentation here.
I've tried to just change the button's Image property during my ToggleStateChanged event, but I can't see how to even reference the desired image.

I was eventually able to figure this out, once I saw how the Designer auto-code was assigning images to these buttons. You first have to add the images/icons to the project's Resources.resx file.
Then, the ToggleStateChanged event should look like this:
private void myToggleButton_ToggleStateChanged(object sender, StateChangedEventArgs args)
{
RadToggleButton myButton = (RadToggleButton)sender;
switch (args.ToggleState)
{
case ToggleState.On:
myButton.Image = global::myProject.Properties.Resources.toggleOn;
break;
case ToggleState.Off:
myButton.Image = global::myProject.Properties.Resources.toggleOff;
break;
default:
break;
}
}

Related

MVVM-Block Tab key in WPF

I am working in WPF with MVVM. I implemented WPF Extended Toolkit and I use ChildWindow, when I open the ChildWindow the property IsModal is enabled. But this property does not block navigating with Tab.
I need block the navigating with Tab when the ChildWindos is open.
I tried with Focusable property but does not serve.
I understand your issue is with the tab in the background when show the ChildWindow.
You should try modifying the property KeyboardNavigation.TabNavigation of de Window.
If you use MVVM pattern do something like this in the XAML:
<Window
KeyboardNavigation.TabNavigation="{Binding TabNavigationMode}"
>
In the ViewModel:
private KeyboardNavigationMode _tabNavigationMode;
public KeyboardNavigationMode TabNavigationMode
{
get { return _tabNavigationMode; }
set { _tabNavigationMode = value; RaisePropertyChanged("TabNavigationMode");
}
And create a Method like this that is invoked when you open and close the Child Window
public void IsTabNavigationEnable(bool isEnable)
{
if (isEnable) TabNavigationMode = KeyboardNavigationMode.Contained;
else TabNavigationMode = KeyboardNavigationMode.None;
}
I tried it and it works fine. The tab is disabled in the background but not in the ChildWindow.
This is a known issue and the extended tool kit team needs to work on it.In the mean time if you still like to implement this feature using the ChildWindow I would suggest to subscribe to PreviewKeyDown event and manually change the behavior of the tab and arrow keys when the childwindow goes modal.
The link for the issue is
https://wpftoolkit.codeplex.com/discussions/252462

How to prevent silverlight validation popup from appearing half off the screen

I have a silverlight 4 application which has some text boxes that are as wide as the page.
When there is a validation error, a popup is displayed when the user clicks in the control.
The problem is - it only shows the popup for these long text boxes to the left of the text box. It wont go above or below and so as a consequence, most of the popup is displayed out of the page so its chopped off.
I know that I can re-template the text box and try to adjust the popup myself, but before doing that I just wanted to check to see if someone knew of a simple property or something that I can use to prevent that from happening?
Cheers
Rod.
Good question. I guess I would try to solve this via a somewhat "intelligent" AttachedProperty.
Pseudo code ahead:
<TextBox ... my:PopupUtils.KeepPopupWithinScreen="True"/>
And the (pseudo c#) code:
public static class PopupUtils
{
// remember: pseudo code, just to get the idea
static AttachedProperty KeepPopupWithinScreen = type: bool, default: false,
onChanged: HandleKeepPopupWithinScreenChanged;
private static void HandleKeepPopupWithinScreenChanged(
DependencyObject obj, bool value)
{
obj.Loaded += HandleTargetElementLoaded;
}
private staic void HandleTargetElementLoaded(object sender, ...)
{
var popup = VisualTreeHelper.GetDecendantOfType<Popup>(sender);
if ( popup != null )
{
var offsetController = new OffsetController();
offsetController.SetBinding(ObservedOffsetProperty,
new Binding("HorizontalOffset"){Source=popup});
offsetController.ControlledTarget = popup;
//now to prevent garbageCollection...
SetAttachedOffsetController(popup,offsetController);
}
}
public static AttachedProperty AttachedOffsetController = type:OffsetController;
}
I do this sometimes, so this pattern is actually working quite nicely. Maybe it feels a bit "unnatural" at first.
Just letting you know of the solution I used to this problem.
It was another StackOverflow question which I have lost the reference to so I do apologise for not referencing it properly, but the problem is caused by the Text Box and other control styles having a fixed position of to the side of the control when displaying the validation message.
I simply had to create a copy of the style and have the popup appear at the top of the controls instead of beside it.
Problem Solved.
Cheers
Rod.

how to Show MDIChild Form on Top of the MDIParent's Controls

I have a MDI-Parent Form with many ChildForms, when I want to add a control on my Parent form, Child form appears under the control, For example I want to add a groupbox and a PictureBox on MDIParent Form, but when I call the Child Form it appears Under these controls.
frmChildForm1.TopMost=true doesn't works either.
I have attached a photo for more description.
What can I do?!
but I want to have an Image as Background
That's possible, you can set the BackgroundImage property of the MDI client control. The only obstacle is that you cannot directly get a reference to that control. You have to find it back by iterating the form's Controls collection. Like this:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
foreach (Control ctl in this.Controls) {
if (ctl is MdiClient) {
ctl.BackgroundImage = Properties.Resources.Lighthouse;
break;
}
}
}
}
Where Lighthouse was a sample image I added as a resource. Change it to use your own. Another common technique is to subscribe the Paint event for that control and draw whatever you want. A gradient is a common choice.

WPF combobox-like custom control

I would like to create custom control that will look like standard WPF ComboBox, but instead of instead of having an ItemsPresenter in the popup there will be another custom control. So, I created a new class that derives from System.Windows.Controls.Control, added a IsDropDownOpen property and created a style that is actually a copy of default ComboBox style (main idea is that the Popup.IsOpen and ToggleButton.IsPressed properties are bound to the IsDropDownOpen property of the control).
The problem is that the Popup is not closed when I click outside of the control.
I took a look at the ComboBox class in the Reflector and found out that ComboBox used some logic to update the IsDropDownOpen property when it loses mouse capture. But that code uses some internal classes. Is there any alternative way to determine if the user clicked outside of the control and close the Popup?
UPD: I didn't find the way to attach a file to post, so I uploaded sample project here
There is a custom control that looks like ComboBox, but it has a TreeView in a popup. When you open popup and click outside of the control it closes automatically, but if you open popup, expand 'Item2' and then click outside the popup isn't closed. The question is how to fix this?
There is the Control.LostFocus event, maybe handling that would be sufficient for this.
This code solves the problem.
In the static contructor:
EventManager.RegisterClassHandler(typeof(CustomComboBox), Mouse.LostMouseCaptureEvent, new MouseEventHandler(OnMouseCaptureLost));
Event handler implementation:
private void OnMouseCaptureLost(object sender, MouseEventArgs e)
{
if (Mouse.Captured != _container)
{
if (e.OriginalSource != _container)
{
Mouse.Capture(_container, CaptureMode.SubTree);
e.Handled = true;
}
}
}

A few AvalonDock styling questions (WPF)

I'm trying to implement AvalonDock into my application, but I'm having trouble figuring out some of the styling techniques. If someone could please help with the following couple of questions, I would be very grateful:
1) Is there a way to remove the main "Close" button from a DocumentPane and instead place individual buttons on the tabs?
2) I have custom-styled buttons in my application that are placed inside DockableContent elements. As long as the DockableContent is docked, the button uses my custom template, but if a pull the DockablePane that contains the DockableContent out and have it floating, the button loses its template. Is there some trick to getting this to hold?
Thanks in advance for your help!
With regard to #2, that seems to be a problem in AvalonDock. I have a TabControl that loses its styling when its dockable content is floated. When docked, styling is restored.
The workaround is to reset the styling on the StateChanged event.
private void OnDockableContentStateChanged (object sender, RoutedEventArgs e)
{
if (uxDockableContent.State == DockableContentState.DockableWindow)
{
foreach (TabItem tabItem in uxTabControl.Items)
{
tabItem.Style = FindResource ("TabItemStyle") as Style;
}
}
}
I had the best luck getting around this by just downloading the source code, making my changes, and recompiling the DLL.

Resources