Property is not triggered second time - wpf

I have 4 polygons
...
xmlns:cm="http://www.caliburnproject.org"
...
<Polygon cm:Message.Attach="
[Event MouseEnter] = [Action OnMouseEnter($eventArgs)];
[Event MouseLeave] = [Action OnMouseLeave($eventArgs)];
[Event MouseDown] = [Action OnMouseDown($eventArgs)]">
<Polygon.ToolTip>
<ToolTip Visibility="{Binding Path=TooltipVisibility}" >
...
on my canvas.
TooltipVisibility code:
public string TooltipVisibility {
get {
if (OtherObject.IsTooltipVisibility) {
return "Visible";
}
else {
return "Hidden";
}
}
}
And IsTooltipVisibility on OtherObject is changed from 3rd place. I tried to execute NotifyOfPropertyChange(() => myObject.TooltipVisibility );, I tried run NotifyOfPropertyChange(() => otherObject.IsTooltipVisibility);.
The funny (however, not really) thing is that if I hover mouse on one polygon it shows tooltip, then I change IsTooltipVisibility (by hitting menu button in the menu bar) to false, it doesn't show tooltip on second polygon, but still shows it on the first one. Third one will not show tooltip. Change to true, 4th one will show, however, 2nd and 3rd won't.
The TooltipVisibility property executed just ones. How can I make it execute all the time?

It's really weird solution. It works. Maybe it should work this way. But I think it's too complex. It should be more straightforward and with less code.
public void SetTooltipVisibility(bool toShowTooltip) {
if (toShowTooltip) {
_tooltipVisibility = "Visible";
}
else {
_tooltipVisibility = "Hidden";
}
NotifyOfPropertyChange(() => TooltipVisibility);
}
private string _tooltipVisibility = "Visible";
public string TooltipVisibility {
get {
return _tooltipVisibility;
}
}
And instead of just setting one flag
OtherObject.IsTooltipVisibility = true;
additionality I need to
liistOfMyObjects.Apply(o=>o.SetTooltipVisibility(OtherObject.IsTooltipVisibility));
Not the nicest solution.
I'm waiting for a PRO to show how this should be done in right manner.

Related

SwiftUI, IF inside ForEach loop to display different text

I am having a hard time displaying certain text in a ForEach loop in SwiftUI.
I have an array that I iterate over with ForEach. The problem is, I need to have certain text displayed for whether or not a specific date is selected in a calendar.
For example:
ForEach(reminders) { reminder in
if(Calendar.current.isDate(reminder.beginDate, equalTo: selectedPassedDate) {
HStack {
Text("The Text for the specific day")
} else {
Text("The text for the unspecific day")
}
}
}
When the array has more than 2 elements, the text for the non-matching day is being displayed multiple times (since it is inside the foreach). This makes sense, but I am trying to figure out a way to only display ONE of the "unspecified day" text instead of many.
I have tried adding a simple bool to display the other text, but I cannot change the state of it without a button or something.. I need to do this totally programmatically. I've also moved the else statement outside of the loop but again the secondary text is being displayed in the current day selection.
What can I do to solve this issue? If I can programmatically set the bool while conforming to SwiftUIs view then I can solve this, but I'm not sure how.
you could try something like this:
class FirstTime: ObservableObject {
var value = true
}
struct MyView: View {
...
#State var firstTime = FirstTime()
var body: some View {
VStack {
ForEach(reminders) { reminder in
HStack {
if (Calendar.current.isDate(reminder.beginDate, equalTo: selectedPassedDate) {
Text("The Text for the specific day")
} else {
if firstTime.value {
showTextOnce()
}
}
}
}
}
}
func showTextOnce() -> some View {
firstTime.value = false
return Text("The text for the unspecific day")
}
}

How to check dynamically rendered checkboxes

I'm rendering some checkboxes dynamically, but currently I'm only able to check the first box, and all other boxes operate the first one. How do I get the boxes to work independently of each other?
This is typescript in React. I've tried changing the interface I'm referencing in the function, thinking I was referencing the wrong thing, but none of those worked.
This is the function:
handleCheckboxClick = (entitlement: IApiEntitlements, checked: boolean): void => {
if (checked === true) {
this.selectedEntitlementIDs.push(entitlement.id);
} else {
const index: number = this.selectedEntitlementIDs.indexOf(entitlement.id);
this.selectedEntitlementIDs.splice(index, 1);
}
//tslint:disable-next-line:prefer-const
let entitlementChecked: IEntitlementChecked = this.state.entitlementChecked;
entitlementChecked[entitlement.id] = checked;
let selectAll: boolean = false;
if (this.selectedEntitlementIDs.length === this.state.responses.apiResponses.apiClients.length) {
selectAll = true;
}
this.setState({
entitlementChecked: entitlementChecked,
selectAll: selectAll
});
console.log(this.selectedEntitlementIDs, 'hi');
console.log(entitlementChecked, 'hello');
}
And this is where it's being called:
return (
<Checkbox
checked={this.state.entitlementChecked[entitlement.id]}
data-ci-key={entitlement.id}
id='api-checkbox'
key={entitlement.id}
labelText={entitlement.label}
onChange={this.handleCheckboxClick}>
</Checkbox>
);
I expect each checkbox to be able to be checked, but currently on the first one works, and all others check or uncheck that first one.
You shouldn't keep an array as a property on the class that keeps track of selected items, this isn't tied to the React lifecycle and could potentially not update the view when you want to. Instead you should just use your map (entitlementChecked) you already have to determine if something is checked or not.
handleCheckboxClick(id) {
this.setState(prevState => ({
entitlementChecked: {
...prevState.entitlementChecked,
[id]: !prevState.entitlementChecked[id]
}
}));
}
When calling the handler method, you can just pass the id through that you need specifically.
onChange={this.handleCheckboxClick.bind(null, item.id)}
Here's a rudimentary example for more detail :)

Want to disable the command based on canexecute

I have a simple DelegateCommand from Telerik. I have a case,
I have a screen with a textbox and a button. Whenever I click save the button should be disabled and should not accept any clicks until the operation completes.
The implementation is as below,
public DelegateCommand SaveRemarksCommand
{
get
{
return _saveRemarksCommand = new DelegateCommand((r) =>
{
CanSaveRemarks = false;
SaveRemarksCommand.InvalidateCanExecute();
SaveRemarks(null);
},
(result) =>
{
return CanSaveRemarks;
});
}
}
Here I am manipulating the CanSaveRemarks as false until the SaveRemarks executes.
The problem now is if I click multiple times fast on the button the button accepts multiple clicks saving duplicates.
You can additionally check the CanSaveRemarks property in your command handler.
This property is already available to you, so you don't need to introduce something else.
new DelegateCommand((r) =>
{
if (!CanSaveRemarks)
{
return;
}
CanSaveRemarks = false;
try
{
SaveRemarksCommand.InvalidateCanExecute();
SaveRemarks(null);
}
finally
{
CanSaveRemarks = true;
}
},
(result) =>
{
return CanSaveRemarks;
});

ReactiveUI "Compelling Example" how to refresh the search results

My question is in regards to the "Compelling Example" given for ReactiveUI where as a person types in a search bar, the search occurs asynchronously. Suppose though I wanted to provide my user with a way to refresh the current search results. I could just ask them to backspace in the search bar and retype their last character. However, they are asking for a "Refresh" button because it's not obvious to them how to refresh the current results.
I can't think of how to do this within the context of the example:
public class TheViewModel : ReactiveObject
{
private string query;
private readonly ObservableAsPropertyHelper<List<string>> matches;
public TheViewModel()
{
var searchEngine = this.ObservableForProperty(input => input.Query)
.Value()
.DistinctUntilChanged()
.Throttle(TimeSpan.FromMilliseconds(800))
.Where(query => !string.IsNullOrWhiteSpace(query) && query.Length > 1);
var search = searchEngine.SelectMany(TheSearchService.DoSearchAsync);
var latestResults =
searchEngine.CombineLatest(search, (latestQuery, latestSearch) => latestSearch.Query != latestQuery ? null : latestSearch.Matches)
.Where(result => result != null);
matches = latestResults.ToProperty(this, result => result.Matches);
}
public string Query
{
get
{
return query;
}
set
{
this.RaiseAndSetIfChanged(ref query, value);
}
}
public List<string> Matches
{
get
{
return matches.Value;
}
}
}
Does anyone have any suggestions on how I could capture a command from a button and re-execute the existing search without clearing out their current search text?
You can merge the existing observable of Query changes with a new observable that returns the current Query when the refresh button is pressed.
First a command for the refresh button:
public ReactiveCommand<Unit, String> Refresh { get; private set; }
Then you create the command and assign it, and create a merged observable of the two observables:
Refresh = ReactiveCommand.Create<Unit, String>(() => Query);
var searchEngine = Observable.Merge(
this.ObservableForProperty(input => input.Query).Value().DistinctUntilChanged(),
Refresh)
.Throttle(TimeSpan.FromMilliseconds(800))
.Where(query => !string.IsNullOrWhiteSpace(query) && query.Length > 1);
The rest can stay unchanged.

WPF CheckBox Binding - Altering value in Property Set does not affect UI

i have a simple checkbox
<CheckBox IsChecked="{Binding ForceInheritance}"/>
In Code i have a class with "INotifyPropertyChanged" and the Property
public bool ForceInheritance
{
get { return forceInheritance; }
set
{
if (forceInheritance != value)
{
value = SomeTest();
if (forceInheritance != value)
{
//something is done here.
}
OnPropertyChanged("ForceInheritance");
}
}
}
If SomeTest() returns !value so the underlying data does not have to change the CheckBox still changes its IsChecked state.
For example:
ForceInheritance is false.
CheckBox is clicked.
SomeTest() returns false.
--> The underlying data is not changed
--> I would expect the CheckBox to stay unchecked
The CheckBox gets checked.
What can i do to not change the IsChecked state of the CheckBox when the setter does not actually change the value?
I thought the "OnPropertyChanged("ForceInheritance");" would do the trick but it didn't.
Thank you for your time.
This problems occurs because when you click checkbox it s value is changed. Then binding causes property set. There you manipulate with its value and hope that calling OnPropertyChanged will cause back binding action updating checkbox value. But doesn't work because updating control after updating property may produce infinite updating loop. To prevent this PropertyChanged is ignored.
What can you do is to move some logic of your property setter to binding validation rule. You can read here about validation rules or leave a comment if you need more information or examples.
Hope it helps.
I would expect the CheckBox to stay unchecked
Now, it seems that CheckBox working as you expect. Something must have changed recently because it works differently now.
Previously
When I needed to correct a value in the setter I use Dispatcher.
public bool IsActive
{
get => _isActive;
set
{
if (_isActive != value)
{
if (value)
{
if (!CanActive())
{
Application.Current.Dispatcher.BeginInvoke((Action)(() => IsActive = false));
return;
}
}
_isActive = value;
OnPropertyChanged(nameof(IsActive));
}
}
}
Now
Now, I don't have to change a value back to false. This works even if I remove OnPropertyChanged call, because after the setter is called, the getter is also called. It looks like the CheckBox is now correcting its state.
public bool IsActive
{
get => _isActive;
set
{
if (_isActive != value)
{
if (value)
{
if (!CanActive())
{
return;
}
}
_isActive = value;
OnPropertyChanged(nameof(IsActive));
}
}
}

Resources