Replies: 3 comments 2 replies
-
I don't think it's something that can be differentiated, as it looks like it's mixing concerns. I think you would be better off by binding the UI to a separate property, doing your extra work/validation, and have the "system-side" property notify the UI one. And keep both of them updated. Now, since it is a bool... I know I once had to do a "fake" read-only property with getter and a no-op setter in order to keep using a two-way binding on a toggle button's IsChecked property with a Command bound to the toggle button. If it was a one-way binding, the state wasn't getting updated correctly. I am not a senior professional UI developer though, it's only thoughts from my experience with the mvvm toolkit. |
Beta Was this translation helpful? Give feedback.
-
You can extend your property to cover more states and you can provide an // BoolModel.cs
public partial class BoolModel : ObservableObject
{
public enum MyEnum
{
FalseCode, // false set by code behind
FalseUser, // false set by UI
TrueCode, // true set by code behind
TrueUser // true set by UI
};
[ObservableProperty]
MyEnum myValue = MyEnum.FalseCode;
} // BoolConverter.cs
public class BoolConverter : IValueConverter
{ public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is not null && value is BoolModel.MyEnum myEnum)
{
return myEnum switch
{
BoolModel.MyEnum.FalseCode => false,
BoolModel.MyEnum.FalseUser => false,
BoolModel.MyEnum.TrueCode => true,
BoolModel.MyEnum.TrueUser => true,
_ => throw new NotImplementedException(),
};
}
return false;
}
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is not null && value is bool boolValue)
{
return boolValue ? BoolModel.MyEnum.TrueUser: BoolModel.MyEnum.FalseUser;
}
return BoolModel.MyEnum.FalseUser;
}
} <!-- UserBoolPage.xaml -->
<ContentPage
x:Class="Maui.StackOverflow.UserBoolPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Maui.StackOverflow"
Title="UserBoolPage">
<VerticalStackLayout x:DataType="local:BoolModel">
<CheckBox IsChecked="{Binding MyValue, Mode=TwoWay, Converter={local:BoolConverter}}" />
<Label Text="{Binding MyValue, Converter={local:BoolConverter}}" />
<Label Text="{Binding MyValue}" />
</VerticalStackLayout>
</ContentPage> |
Beta Was this translation helpful? Give feedback.
-
xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
|
Beta Was this translation helpful? Give feedback.
-
Hello,
I have a boolean ObservableProperty in my application that can be updated either via the UI by the user or the system may update its value automatically. When the property is updated via the UI I need to update the rest of the application about this change. On the other hand, when the system makes the update I only need to update its value in the UI and I don't have to do any processing.
Is there a way to differentiate if the OnPropertyChanged event was triggered by code or by interaction with the UI?
Thanks,
Miguel
Beta Was this translation helpful? Give feedback.
All reactions