How to bind tooltip to a button for different conditions - wpf

How to bind tooltip dynamically for different conditions
we have 2 Projects in the solution v are using PRISM framework
GeneralBL contains the business logic and
StudentManagementUI contains the Usercontrols ,views and ViewModels
Have StudentStatusUserControl.xaml.cs contains a Telerik RadButton
<telerik:RadButton Name="button1" Content="Stauses" Height="24" HorizontalAlignment="Left" VerticalAlignment="Top" Width="112" FontSize="12" Margin="2,2,2,2"
prism:Click.Command="{Binding ButtonstatusCommand}">
this is enabled for a specific condition & when it is disabled we have to show the mouse hover or tooltip info depending on the condition
In the StudentStatusViewModel.cs
private bool CanExecuteButtonStatusCommand(object o)
{
return SharedLogicBL.CanExecuteButtonStatusCommand(controller,dataService, _selectedItem);
}
SharedLogicBL.cs in GeneralBL project
public static bool CanExecuteUnplannedInspection(BaseController controller, DataService dataService, SDataItem selectedItem)
{
if(controller.currentuser.Isallowed())
{
if(selectedItem!=null)
{
Orders = dataservice.GetOrders(selectedItem);
return !Orders.Any();
}
}
else
return false;
}
In the above method check to see if the user has the rights, if not Tooltip on the button "User doesn't have the rights"
Let first condition is true , in the Orders.Any() returns false then we should display "the selected student has no orders"
Also have a dependency property in the StudentStatusUserControl.xaml.cs for this StudentStatusUserControlBL in the GeneralBL project

Create a public property in your viewmodel that you can databind the telerik button tooltip text to.
public string Button1TooltipText
{
get {
if (!controller.currentuser.Isallowed())
{ return "User doesn't have the rights" }
else
{
if (!SharedLogicBL.CanExecuteButtonStatusCommand(controller, dataService, _selectedItem))
return "the selected student has no orders";
else
return "Execute the unplanned inspection";
}
}
}
Since this property depends on the currently selected item, you'll need to call NotifyPropertyChanged("Button1TooltipText") when _selectedItem changes.

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.

WPF Button with Multiple Content

I am new in WPF,I am using Button control in WPF windows application, I am displaying its content from Database,
I have its click event binded with its Name,like this
<Button Content="{Binding FirstName}" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="628,178,0,0" x:Name="btn1Click"/>
This button have different content for each user.
This is my FirstName property
string _fname;
public string FirstName
{
get { return _fname; }
set { _fname= value; NotifyOfPropertyChange("FirstName"); }
}
I want to get ID of the clicked user, how can I put UserID in content(with firstname)?
or in x:Name so I can identify which user is clicked?
Or I can use some hidden field for it and how?
You can bind a property to commandParameter and can check where you have binded the command to the event handler. like
<Button Command="{Binding BtnClickCommand}" CommandParameter="{Binding Id}" />
In ViewModel of this view:
BtnClickCommand=new RelayCommand(o=>BtnClickEvent((string)o));// Cast you id to whatever data type u have.
private void BtnClickEvent(string Id)
{
//Here you got your id and do what u want..
}
Assign DataContext property of button's parent with the user object then in the click event of a button write following code.
private void Button_Click(object sender, RoutedEventArgs e)
{
Button item = sender as Button;
if(item != null)
{
//you will get user details here by using item.DataContext property
User clickedUser = item.DataContext as User;
}
}
Here you will get all the details of Clicked User.

Cancel a user edit from property set in the ViewModel?

quite a newbie question, i'm sure, but i wasn't able to find an answer...
I have a control (in this case- a combo box) which is bound to a ViewModel property:
<ComboBox
x:Name="methodTypeCmb"
Grid.Row="0" Grid.Column="2"
ItemsSource="{Binding Path=AllNames, Mode=OneTime}"
SelectedItem="{Binding Path=Name, ValidatesOnDataErrors=True, Mode=TwoWay}"
Validation.ErrorTemplate="{x:Null}"
/>
In my ViewModel, when this property changes, I want to ask the user to confirm the change.
If the user clicks 'no', I want to cancel the change.
However, I must be doing something wrong, because my view doesn't revert back to the previous value when the change is cancelled.
The ViewModel's property:
public string Name
{
get { return m_model.Name; }
set
{
if (MessageBox.Show("Are you absolutely sure?","Change ",MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
// change name
}
base.OnPropertyChanged("Name");
}
}
Because you are cancelling within the scope of the text changing event, wpf ignores the property changed event. You must call it from the dispatcher
Dispatcher.CurrentDispatcher.BeginInvoke((ThreadStart)delegate
{
OnPropertyChanged("Name");
});
You should leave your existing "OnPropertyChanged("Name");" at the bottom of the function just add the above line to the block where you are cancelling
EDIT: The following code works I have tested it
public string Newtext
{
get
{
return this._newtext;
}
set
{
if (MessageBox.Show("Apply?", "", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
this._newtext = value;
this.OnPropertyChanged("Newtext"); //Ignored
}
else
{
Dispatcher.CurrentDispatcher.Invoke((ThreadStart)delegate
{
OnPropertyChanged("Newtext");
});
}
}
}

Binding view with model (view doesn't update)

i'm implementing something, that if i select something in my listbox, some textboxes come visible. So i can fill in some details of the selected item. I already implemented a visibilityconverter and this is my code of xaml and viewmodel:
The items in the listbox are objects of class Question
public Question SelectedQuestionDropList
{
get { return selectedQuestionDrop; }
set
{
selectedQuestionDrop = value;
OnPropertyChanged("SelectedQuestionDropList");
Visible = true;
}
}
this is my property of Visibility:
public Boolean Visible
{
get { return visible; }
set { visible = value; }
}
my xaml looks like this:
<ListBox SelectedItem="{Binding Path=SelectedQuestionDropList, UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"
DisplayMemberPath="Description"
/>
<TextBox Height="23" Visibility="{Binding Path=Visible, Converter={StaticResource boolToVis},UpdateSourceTrigger=PropertyChanged,Mode}" />
But i have a problem, when i select something, the property visible is set to true, but the visibility of the textbox stays false .. so my view doesn't update with the viewmodel.
someone who knows what i am doing wrong?
In order for the Visibility Binding to update you have to change your property to call OnPropertyChanged:
public Boolean Visible
{
get { return visible; }
set
{
visible = value;
OnPropertyChanged("Visible");
}
}

Resources