How to implement Multistate CheckBox in WPF for MVVM - wpf

I have a requirement for which i need a MultiState Checkboxes in WPF...
Also as i am using MVVM so handling binding and Commands should follow the same as i will use it in my Views.
I have seen a Multistate checkbox in DotnetNuke(made in ASP.NET) but how to make it in WPF
some inputs in this regards will be helpful
Some example will be great...

Checkboxes have specific functionality (checked, unchecked, and optionally indeterminate).
Based on your comment, I would think it would be easiest to just do it as a button. I don't have time now to test out an actual example, but here some pseudocode to get you going:
XAML
<Button Command="{Binding ToggleDecisionState}">
<Button.Content>
<Image Source="{Binding CurrentDecisionIcon}" />
<Button.Content>
</Button>
ViewModel (leaving out MVVM implementation details)
enum Decisions
{
Agree,
Disagree,
Maybe,
DoNotKnow
};
public Decisions CurrentDecision
{
get {}
set {}
}
public RelayCommand ToggleDecisionStateCommand
{
// In here, call code to execute toggle
if (mCurrentDecision == Decisions.DoNotKnow)
CurrentDecision = Decisions.Agree;
else
CurrentDecision += 1;
}
public ImageSource CurrentDecisionIcon
{
get
{
ImageSource img = [some default image];
switch (mCurrentDecision)
{
case Decisions.Agree:
img = [path to Agree icon];
break;
// Other cases here
}
return img;
}
}

if you want to use 3-state check box, you must change the behind boolean value(in ViewModel)
to bool? type.
public bool? IsEnabled { get; set; }
and Set this property for CheckBox
IsThreeState = True;

Related

WPF Button binding to ICommand not firing

I have a Button bound to an ICommand interface but it isn't being fired when I run the application.
The button should be disabled when the app runs, putting a breakpoint in the ICommand or CanUpdate but it isn't being hit.
The ICommand seems to have been implemented correctly as far I can see - have substituted value in CanUpdate for simplicity...
Scratching my head to workout what is missing?....
XAML
<StackPanel Orientation="Horizontal" Grid.Row="1" Grid.Column="1">
<RadioButton Width="64" IsChecked="{Binding Passed}" GroupName="T1">Yes</RadioButton>
<RadioButton Width="64" IsChecked="{Binding Passed, Converter={StaticResource InverseBoolRadioConverter}}" GroupName="T1" >No</RadioButton>
</StackPanel >
Button Command="{Binding UpdateHasPassed}" Content="Update"></Button>
Code-Behind:-
private RelayCommand hasPassed;
public bool Passed
{
get
{
return passed;
}
set
{
if (passed !=value )
{
passed = value;
OnPropertyChanged();
}
}
}
public ICommand HasPassed
{
get
{
if (hasPassed == null)
{
haspassed = new RelayCommand( param => CanUpdate());
}
return haspassed;
}
}
private bool CanUpdate()
{
return (1 != 2)
}
You're on the right track looking into INotifyPropertyChanged. I would also recommend reading up on WPF data bindings, the ICommand interface (and specifically creating a RelayCommand, more on that later), and MVVM design.
The benefit of WPF data bindings and ICommand is that you can control when the button gets enabled or disabled, based on your conditional criteria (i.e. name has changed from its original value). With the tips mentioned here, you should be able to do what you want in short time. Just google each of the topics and you'll get what you need.

Xamarin Forms create a Clicked Event for CheckBox Control

so I am trying to use the CheckBox Control with MVVM logic. However the only event existing is CheckedChanged, which also triggers if I am navigating from the page for example.
I am searching for a Clicked event, like a button has.
Do I need to create a custom control with custom renderer, or is there a better solution?
Looking forward to replies.
[EDIT]
To provide more information about my issue, I will put code examples below.
My CheckBoxes are inside a ListView. Each CheckBox is defined like this in XAML:
<CheckBox
IsChecked="{Binding IsCompleted}">
<CheckBox.Behaviors>
<prism:EventToCommandBehavior
EventName="CheckedChanged"
Command="{Binding SubTaskStateChangedCommand}"/>
</CheckBox.Behaviors>
</CheckBox>
The ItemSource of the ListView has a property IsCompleted and a DelegateCommand SubTaskStateChangedCommand. It is worth mentioning that i am using Prism.
In the constructor:
SubTaskStateChangedCommand = new DelegateCommand(OnSubTaskStateChangedCommandExecuted);
Class SubTaskModel:
public DelegateCommand SubTaskStateChangedCommand { get; set; }
private void OnSubTaskStateChangedCommandExecuted()
{
//Do something
}
private bool _isCompleted;
public bool IsCompleted
{
get { return _isCompleted; }
set { _isCompleted = value; OnPropertyChanged(); }
}
So when I am navigating to another page in my ViewModel using the Prism NavigationService, the OnSubTaskStateChangedCommandExecuted Method gets triggered. However, I noticed that when this happens, the IsCompleted value DOES NOT change.

Fluent Ribbon SelectedTabItem or SelectedTabIndex

I'm using Fluent Ribbon in a MVVM design pattern, with a View/ViewModel. My goal is to change the body according to which tab is selected. I could use either SelectedTabItem or SelectedTabIndex and lookup the corresponding view. However, neither fire when the tab selection changes. In the View I have...
<Fluent:Ribbon Grid.Row="0" SelectedTabItem="{Binding SelectedRibbonTab}" SelectedTabIndex="{Binding SelectedRibbonTabIndex}">
</Fluent:Ribbon>
<ContentControl Grid.Row="1" Content="{Binding RibbonTabContent}"/>
In the ViewModel I have...
// Used both Item and Index for troubleshoothing, but need only one or the other for implementation
private IDictionary<string, FrameworkElement> RibbonTabViews;
private List<FrameworkElement> RibbonTabViewsList;
public RibbonTabItem SelectedRibbonTab
{
get
{
return selectedRibbonTab;
}
set
{
Update(() => SelectedRibbonTab, ref selectedRibbonTab, value, false);
UpdateContentControl();
}
}
public int SelectedRibbonTabIndex
{
get
{
return selectedRibbonTabIndex;
}
set
{
Update(() => SelectedRibbonTabIndex, ref selectedRibbonTabIndex, value, false);
UpdateContentControl(value);
}
}
public FrameworkElement RibbonTabContent
{
get { return ribbonTabContent; }
set { Update(() => RibbonTabContent, ref ribbonTabContent, value, false); }
}
protected void UpdateContentControl()
{
RibbonTabContent = RibbonTabViews[SelectedRibbonTab.Header.ToString()];
}
protected void UpdateContentControl(int index)
{
RibbonTabContent = RibbonTabViewsList[index];
}
I know I don't need both bindings but for the sake of troubleshoot I have both Item and Index. I would think in the ViewModel SelectedRibbonTab and SelectedRibbonTabIndex would be called each time the tab changed. Unfortunately, that doesn't appear to be the case. I have breakpoints at the setters and getters for each and neither are hit when changing tabs. What am I missing? I've been using this approach for years with Microsoft Ribbon but for some reason these don't fire in Fluent Ribbon. Any help would be great, thank you.
You have to set the binding mode to TwoWay to get updated values in your ViewModel.

WPF MVVM - how to clear a list when bound to an AsyncCommand

I have a ListView that is bound in the XAML to an AsyncCommand defined in the ViewModel. All examples are cut down for brevity.
XAML
<listView ItemSource="{Binding Path=ReportCommand.Execute.Result}" />
<Button Command="{Binding ReportCommand}">Click</Button>
MVVM
In the constructor:
ReportCommand = new AsyncCommand<List<MyPoco>>(() => LoadReport());
In the class:
public IAsyncCommand ReportCommand { get; private set; }
private async Task<List<MyPoco>> LoadReport()
{
return await _service.GetListOfPocos();
}
(fyi - this is based on Stephen Cleary's MSDN article)
okay, so far, all well and good.
However, on another control on the Window, the user does something that requires this ListView to be cleared (for example, they log out). I know how I can get my various ViewModels to talk to each other behind the scenes, but how do I get to clear this "command" so that the List that is bound to by ListView is empty?
Thanks
Griff
Could you not store the result of the async command in another property in your viewmodel, and bind your ListView to that property? That would give you complete control over how and when the content changes. Something like this:
public ViewModelConstructor()
{
ReportCommand = new AsyncCommand(() => LoadReport());
}
public IAsyncCommand ReportCommand { get; private set; }
private async Task LoadReport()
{
MyPocoList = await _service.GetListOfPocos();
}
public List<MyPoco> MyPocoList
{
get { ... }
set { ...; RaisePropertyChanged("MyPocoList"); }
}
and your XAML would change to:
<ListView ItemSource="{Binding MyPocoList}" />
<Button Command="{Binding ReportCommand}">Click</Button>
If you needed to clear the listbox, you can simply set MyPocoList = null;
I'm assuming you're implementing INotifyPropertyChanged in your ViewModel with this example.

How can I prevent a ToggleButton from being Toggled without setting IsEnabled

I have a list of ToggleButtons being used as the ItemTemplate in a ListBox similar to this answer using the MultiSelect mode of the Listbox. However I need to make sure at least one item is always selected.
I can get the proper behavior from the ListBox by just adding an item back into the ListBox's SelectedItems collection on the ListBox.SelectionChanged event but my ToggleButton still moves out of its toggled state so I think I need to stop it earlier in the process.
I would like to do it without setting IsEnabled="False" on the last button Selected because I'd prefer to stay with the Enabled visual style without having to redo my button templates. Any ideas?
You can override the OnToggle method to prevent toggling the state, by not calling the base implementation :
public class LockableToggleButton : ToggleButton
{
protected override void OnToggle()
{
if (!LockToggle)
{
base.OnToggle();
}
}
public bool LockToggle
{
get { return (bool)GetValue(LockToggleProperty); }
set { SetValue(LockToggleProperty, value); }
}
// Using a DependencyProperty as the backing store for LockToggle. This enables animation, styling, binding, etc...
public static readonly DependencyProperty LockToggleProperty =
DependencyProperty.Register("LockToggle", typeof(bool), typeof(LockableToggleButton), new UIPropertyMetadata(false));
}
Have you tried using RadioButtons instead? It normally can't be deselected without selecting another one. It can also be styled to look like a ToggleButton:
<RadioButton Style="{StaticResource {x:Type ToggleButton}}"/>
Or, if you already have a Style for it, just make it BasedOn="{x:Type ToggleButton}". Note that the Visual Studio Editor shows an error in the first case, but it compiles and works fine.
This is hackey, but if you don't want custom code you could always use the property "IsHitTestVisible", when you don't want them to uncheck it, simply set IsHitTestVisible equal to false. However, they may be able to tab to the control and toggle it using the space bar.
Thomas's answer works fine, but you don't even need the extra dependency property. Your button will update correctly if you have the class inherit from ToggleButton so you can override the OnToggle method, and you change the IsChecked bound property on the ViewModel.
Xaml:
<myControls:OneWayFromSourceToTargetToggle x:Name="MyCustomToggleButton"
Command="{Binding Path=ToggleDoStuffCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Mode=Self}}"
IsChecked="{Binding Path=ToggleIsCheckedConditionVar,
Mode=OneWay}"
/>
Added ToggleButton Class:
public class OneWayFromSourceToTargetToggle : ToggleButton
{
/// <summary>
/// Overrides the OnToggle method, so it does not set the IsChecked Property automatically
/// </summary>
protected override void OnToggle()
{
// do nothing
}
}
Then in the ViewModel just set bool ToggleIsCheckedCondition to true or false. This is a nice way to do it because you are following good MVVM practices.
ViewModel:
public bool ToggleIsCheckedCondition
{
get { return _toggleIsCheckedCondition; }
set
{
if (_toggleIsCheckedCondition != value)
{
_toggleIsCheckedCondition = value;
NotifyPropertyChanged("ToggleIsCheckedCondition");
}
}
}
public ICommand ToggleDoStuffCommand
{
get {
return _toggleDoStuffCommand ??
(_toggleDoStuffCommand = new RelayCommand(ExecuteToggleDoStuffCommand));
}
}
private void ExecuteToggleDoStuffCommand(object param)
{
var btn = param as ToggleButton;
if (btn?.IsChecked == null)
{
return;
}
// has not been updated yet at this point
ToggleIsCheckedCondition = btn.IsChecked == false;
// do stuff
}
}
Adding a little bit to #Joachim-Mairböck's great answer in case you want to do the same programmatically:
new RadioButton {
...
GroupName = "myButtonGroup"
Style = Application.Current.TryFindResource(typeof(ToggleButton)) as Style
...
}

Resources