XPlatform .NET UI framework (For best experience access this chat on Telegram https://t.me/Avalonia)
Hi. Got a problem with bindings for templated controls.
My control uses a check box:
<CheckBox
Classes="OptionTitle"
Content="{TemplateBinding Title}"
IsChecked="{TemplateBinding ShowOption}" />
with the property declared in the backing code as:
public static readonly StyledProperty<bool> ShowOptionProperty =
AvaloniaProperty.Register<OptionTitle, bool>(nameof(ShowOption));
public bool ShowOption
{
get => GetValue(ShowOptionProperty);
set => SetValue(ShowOptionProperty, value);
}
However, my binding is not receiving update notifications:
<controls:OptionTitle
ShowOption="{CompiledBinding ShowOptionX}"
Title="X" />
this.WhenAnyValue(vm => vm.ShowOptionX).Subscribe(_ => ...);
never fires.
If, in my VM constructor, I do ShowOptionX = true;
, the WhenAnyValue
observable fires just the once, but never again.
Any ideas what I'm doing wrong?
IsChecked="{TemplateBinding ShowOption, Mode=TwoWay}
Robert
Thanks, I gave this a shot but it was still preventing the textbox from receiving input when I tried it. In any case I solved it now by making the commands unexecutable when that textbox is active. Not ideal and only works for my situation, but atleast it works around my impedement (re @timunie: Robert I found that I can disable parent key bindings if I place another one inside TextBox like:
<Window.KeyBindings>
<KeyBinding Command="{Binding DoSometingCommand}" Gesture="Ctrl+C"></KeyBinding>
</Window.KeyBindings>
<Design.DataContext>
<vm:MainWindowViewModel/>
</Design.DataContext>
<StackPanel>
<TextBox>
<TextBox.KeyBindings>
<KeyBinding Command="{Binding DoNothingCommand}" Gesture="Ctrl+C"></KeyBinding>
</TextBox.KeyBindings>
</TextBox>
<TextBlock Text="{Binding Count}"></TextBlock>
<Button />
</StackPanel>
my ViewModel:
public partial class MainWindowViewModel : ObservableObject
{
[ObservableProperty]
private int _Count;
[RelayCommand]
void DoSometing() => Count++;
[RelayCommand]
void DoNothing(){}
})
Type
to be indexed also specified - which has really big implications for the inheritance hierarchy of the classes in question.