In my project (Silverlight5 with MVVM), i need to hide the Grid using button click event. here the button also located in the grid. Is there anyway to hide this grid..?
Your viewmodel should contain property (for example, IsGridVisible):
public bool IsGridVisible
{
get {return _isGridVisible;}
set {
_isGridVisible = value;
OnPropertyChanged("IsGridVisible");
}
}
Then, you can bind Grid.Visibility to your IsGridVisible property, thought BoolToVisibilityConverter.
And then, you can simply switch state in your button`s command handler.
You can use the visibility property of the grid.
grid.Visibility = Visibility.Collapsed;
Related
I have a listbox with several controls. Each control contains an custom autocompletebox which contains a System.Windows.Controls.AutoCompleteBox. When I right click on the control a custom contextm menu shows up. But with a right click on the textbox there appears the default contextmenu of the TextBox (with Copy, Cut and Paste).
My goal is to show my custom context menu with a right click on the TextBox.
Further informations:
My custom context menu is defined in the DataTemplate of the ListBox but I could define it in the Ressources or somewhere else as well.
I tried:
- when I null the context menu of the custom autocompletebox or the System.Windows.Controls.AutoCompleteBox of it, there is no effect at all
Thanks for every help ;)
You can either bind the context menu property to parent element's context menu or bind to the context menu once you define in the resource.xaml
Try using PreviewMouseDown instead of MouseDown to handle the MouseDown event.
In XAML:
<ListBox Margin="3" PreviewMouseDown="MouseDownOnListBox">
In Code:
private void MouseDownOnListBox(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Right)
{
//Display your context menu
}
}
If you are using PreviewMouseDown on a list, when you click anywhere on the list, this event will be triggered first.
I have a few textboxes on my winform. I have a few buttons on it too. Now when I am typing on one such textbox and clicks a button, then the input focus is lost from the textbox and the button gets the input keyboard focus. That is, the cursor is lost from the textbox the moment we click the button. I dont want this behavior. I want my textbox to retain the cursor within even when I click the button.
The real situation is that I am having textbox and numeric buttons to be only used from touchscreen.
Try creating your own button control that inherits from the standard one but turns off the Selectable style:
public class ButtonEx : Button {
public ButtonEx() {
this.SetStyle(ControlStyles.Selectable, false);
}
}
In your button click event handler(s), explicitly set focus to some other control. Pick any control that you believe would be sensible to gain focus after the button is pressed. For example, set focus to a TextBox, using code like this:
textBox1.Focus();
This will prevent your button from gaining focus when a button is clicked.
In addition, set your button's TabStop property to false.
The other answers suggesting you set the CanFocus property to false won't work because that property is read-only for buttons.
You could set focus to the text box on buttons click event handler like this:
private void Button_Click(...)
{
FocusTextBox();
// Do things...
}
private void FocusTextBox()
{
textBox.Focus();
}
Create custom Button class with Focusable property, set Focusable to false
public class ButtonEx : Button
{
[DefaultValue(true)]
public bool Focusable
{
get { return GetStyle(ControlStyles.Selectable); }
set { SetStyle(ControlStyles.Selectable, value); }
}
}
Looking through the standard WPF commands, such as copy/paste, they seem to all work using one button and act on a textbox.
My question: how do I use commands when I have one button, but I need data to be set in two separate controls(a textbox and a combobox). If the user has written text in textbox, but not selected a combobox value, then CanExecute should fail. This applies if combobox has been set, but not the textbox.
In my case specifically, all these controls are wrapped in a tabitem. As well, I have another tab with only a textbox and a button. I want it to have the same functionality as the first tab, except, instead of checking for the combobox value, it should detect that there is no combobox and pass in a default 'null object' value instead.
The Execute method should call a method in my viewmodel and pass in values from the combobox and textbox. As well, each tab should pass in another unique static value; i think i can handle this using commandparameter though.
How do I make this work? Do I make the parent tab that commandtarget and directly reference its children controls in the can/execute methods?
You need to implement CanExecute method that checks both TextBox databinding value and ComboBox.SelectedItem databinding value.
Take example from your question.
Your TextBox.Text should be databinding to your ViewModel
And as well as your ComboBox.
So your ViewModel should have two Properties:
public string TextBoxCurrentText {get { ...}set {...}}
public string ComboBoxCurrentSelected {get { ...}set {...}}
Then in both Setter, you would do your YourCommmand.RaiseCanExecuteChanged();
So it will execute your CanExecute code piece to determines can your Button be click.
Which can be:
bool YourCommandCanExecute()
{
//Just example
if (!string.IsNullOrEmpty(TextBoxCurrentText) && !string.IsNullOrEmpty(ComboBoxCurrentSelected))
return true;
return false;
}
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;
}
}
}
I have two radiobuttons.
One (rb1) is binded to a property of my ViewModel. If the property is true rb1 is checked when the application is loaded. If the property is false rb1 is unchecked (that's right).
But in the last case, both radiobuttons are unchecked, and I need the second radiobutton (rb2) is checked when property is false. How could I do this??
The issue you are encountering is that the DataBinding is "lost". Let me quote Matt Thalman:
The click would change the UI state of
the buttons correctly (for example,
clicking Bar would uncheck Foo and
check Bar). But I noticed that if the
underlying value of IsFoo and IsBar
ever changed after that point, the
buttons would not have their IsChecked
state updated. Using the Snoop tool,
I discovered that the IsChecked state
had had its state set manually after
clicking on one of the buttons. Once
a dependency property has been set
manually, it loses its Binding. This
is why the IsChecked state was not
being changed when the properties
being bound to were updated.
A simple solution is to subclass the RadioButton class:
public class DataBoundRadioButton : RadioButton
{
protected override void OnChecked(RoutedEventArgs e)
{
// Do nothing. This will prevent IsChecked from being manually set and overwriting the binding.
}
protected override void OnToggle()
{
// Do nothing. This will prevent IsChecked from being manually set and overwriting the binding.
}
}
See this blog entry for more details.