How do I manually Close RadContextMenu on any of the RadMenuItem Click? - wpf

On binding IsOpen Property with ViewModel's BOOL property isn't working.

Related

ToggleButton does not have IsDefault property Wpf

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.

IsEnabled = false makes WPF button disappeared while using caliburn micro

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.

WPF Problems with Custom Control

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.

Multiple Binding

In my WPF Caliburn.Micro application, I have a datagrid and a checkbox with a corresponding ModelView bool property. I need to bind the checkbox to one of the datagrid's fields OneWay (which is easy). But also I want to bind the same checkbox to the property OneWayToSource. Could you please tell me how I can do that? I don't see how Multibinding can help here.
Thanks.
I don't know if this is a checkbox per row of the DataGrid, or a checkbox for a row with a particular id or index. Either way, you can use TwoWay binding, which will be the default anyway if your view model property has a getter and setter.
Your view model property should point to the instance of the record that the DataGrid is binding to.
E.g.
View Model
public ObservableCollection<Item> MyGridItems { get; set; }
public MyViewModel()
{
this.MyGridItems = ...
this.MySpecialItem = this.MyGridItems[0];
}
public Item MySpecialItem
{
get { return this.mySpecialItem; }
set { this.mySpecialItem = value; // notify of property change here }
}
View
<CheckBox IsChecked="{Binding MySpecialItem.MyBooleanProperty}" />

how to populate a datagrid after selecting an item from combobox?

i have an combobox and datagrid when user select a data from combobox the grid will be populated according to that using MVVM and entity framework
Advance thanks
In your ViewModel, create a SelectedItem property which notifies on change as so:
private object _selectedItem
public object SelectedItem
{
get
{
return _selectedItem;
}
set
{
_selectedItem = value;
OnPropertyChanged("SelectedItem")
}
}
Bind your SelectedItem property of your combo box to this property.
Then watch for a change on SelectedItem and change your datagrid's source property accordingly.

Resources