Wpf button has IsDefault property, I can set IsDefault to true or false.
But Toggle Button does not IsDefault property
<ToggleButton x:Name = "tb" Content = "Toggle" Checked = "HandleCheck" **IsDefault="true"**/>
How can i set IsDefault property to ToggleButton, or How to Add IsDefault property to ToggleButton
I want to set IsDefault to false, enter key never trigger it, even after the toggle button is clicked or chosen.
Related
I have a button in my WPF project, which looks like this:
<Button
Grid.Column="1"
HorizontalAlignment="Left"
x:Name="Add"
IsEnabled="False"
BorderThickness="0"
Grid.Row="1" VerticalAlignment="Top"
Height="23" Width="29" Margin="10,0,0,0">
<StackPanel>
<Image Source="\Resources\Pictures\Add.png"
Width="20" Height="20"/>
</StackPanel>
</Button>
In the corresponding ViewModel I have Add method and CanAdd property:
public bool CanAdd
{
get { return true; }
}
public void Add()
{
//some code
}
The problem is that when IsEnabled property is set to False, Button dissapears. I know that this is because of caliburn micro convetions. How can I modify this conventions, so that When I set IsEnabled to False button disables, and when CanAdd is false it becomes collapsed (as it is now)?
You shouldn't set IsEnabled to false in the XAML markup. The Button will be disabled when the CanAdd property returns false only. So if you want to disable the Button, you should implement your view model to return false from CanAdd. The view shouldn't decide when to enable the Button.
If you want to hide the Button you should either set its Visibility property to Hidden or Collapsed in the XAML, or bind it to either a Visibility source property of the view model or to a bool property and use a BoolToVisibilityConverter.
Disabling a Button doesn't hide it.
I defined IsEnabled property for this button in xaml for test purposes only. When The CanAdd is false in ViewModel, button dissapears automatically. To solve this I have to set Visibility property of the button in xaml to Visible and there is no need of additional properties (type of visibility) if you really don't want dissapear button in specific cases.
On binding IsOpen Property with ViewModel's BOOL property isn't working.
I have a Combobox. I would like to make the ComboBox disable and change the color to DarkGray when it's disable. I would like this operation to happen when my application launches.
Upon selection of a button, it should enable and change the color of the combobox to white again.
Any solution for option 1 would be appreciated.
Thanks in advance.
Set the IsEnabled property of the ComboBox to false in your XAML markup:
<ComboBox x:Name="cmb" IsEnabled="False">
<ComboBoxItem>1</ComboBoxItem>
<ComboBoxItem>2</ComboBoxItem>
<ComboBoxItem>3</ComboBoxItem>
</ComboBox>
...and then set it to true in the click event handler for the Button:
<Button Content="Enabled" Click="Button_Click" />
private void Button_Click(object sender, RoutedEventArgs e)
{
cmb.IsEnabled = true;
}
The default colours should suffice. If you want to change these you will have to modify the ComboBox's ControlTemplate as suggested here: https://blog.magnusmontin.net/2014/04/30/changing-the-background-colour-of-a-combobox-in-wpf-on-windows-8/
In form constructor you have to disable combobox and change its back color:
public Form1()
{
InitializeComponent();
comboBox1.Enabled = false;
comboBox1.BackColor = Color.DarkGray;
}
When happens your option 2 (for example using event handler) add this code:
comboBox1.Enabled = true;
comboBox1.BackColor = Color.White;
I have a Custom Control in WPF
public class MyClass: Control, INotifyPropertyChanged
{
private Boolean _hasData;
public Boolean HasData
{
get { return _hasData};
set
{
_hasData = value;
OnPropertyChanged("HasData");
this.Visibility = value ? Visibility.Visible : Visibility.Collapsed;
}
}
#region INotifyPropertyChanged members
// code
#endregion
}
Now here's the thing: should i use a Control Template or a Data Template?
purpose of custom control: showing data that i recieved from a service.
I tried a Custom Template, but the properties of the control aren't bound/connected with the properties of XAML code. The DataContext of my Control Template is the control itself (MyClass).
<ControlTemplate TargetType="{x:Type controls:MyClass}">
<Grid Visibility="{Binding Visibility, UpdateSourceTrigger=PropertyChanged}"}">
<TextBlock Text="Contains Data"/>
</Grid>
</ControlTemplate>
If i check the DataContext (which is the Myclass class), the visibility is Visible or Collapsed. the Visibility of the control (Myclass XAML) won't bind to the DataContext Visibility.
Also, if i set the visibility in the constructor to Collapsed, then it remains being on Collapsed. i also tried triggers and an extra Boolean property Show that is binded to the Grid Visibility (with a converter of course).
What should i do now? i just want that some Control properties like Visibility in MyClass Control have the same value as class MyClass.
In a window got a lisbox which is bind to a list which is of type Employee having checkbox adjacent to each employee name and can be selected individually.
Got another control check box with "select all" option.
I'm able to do "select all" "select none" easily by binding the select all checkbox's isChecked to a property IsSelectAllChecked in my viewModel.
However if select all option is true and every employee items in lisbox is checked..
if one of item I uncheck how can I remove check from select all option checkbox.
<StackPanel Grid.Row="1">
<CheckBox Margin="20,15,0,15" IsChecked="{Binding Path=IsSelectAllChecked}">
<TextBlock VerticalAlignment="Center" Text="Select All" />
</CheckBox>
can anybody advise
The IsChecked of the checkboxes for the employees should also have a binding to an IsSelected property of the employee viewmodel. In the setter of that property you can evaluate if changes to IsSelectAllChecked are needed.
Here is how I would do it:
Here is your Select All Checkbox and the bound property:
<CheckBox Margin="20,15,0,15" Content="Select all"
IsChecked="{Binding Path=IsSelectAllChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
public bool IsSelectAllChecked
{
get
{
return isSelectAllChecked
}
set
{
isSelectAllChecked = value;
// Do some code here to turn all selected booleans to true
SelectAll();
OnPropertyChanged("IsSelectAllChecked"); // Fire OnPropertyChanged event, important for TwoWay Binding!!
}
Note that the Binding for your CheckBox is now in TwoWay mode, and with UpdateSourceTrigger=PropertyChanged . This will allow:
to change value of the bound property from the code ( TwoWay )
to update the CheckBox state when the value is updated ( UpdateSourceTrigger=PropertyChanged )
Up next: here is one of your checkboxes and the equivalent in code
<CheckBox Content="Select one"
IsChecked="{Binding Path=OneOfYourBools, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
public bool OneOfYourBools
{
get
{
return oneOfYourBools;
}
set
{
oneOfYourBools = value;
// If the isAllSelected was true, turn it to false!
if (this.IsSelectAllChecked)
{
this.IsSelectAllChecked = false;
}
OnPropertyChanged("OneOfYourBools"); // Fire OnPropertyChanged event, important for TwoWay Binding!!
}
And this should do the trick : when one bool is updated, the selectAll bool is updated as well, and vice versa