AvaloniaUI UserControl property cannot bind to another property

I’m trying to understand AvaloniaUI property binding (I have strong WPF background). I’ve already checked possible duplicates, with no solution for my problem.

I created a user control, let’s keep is simple:

public partial class UserBox : UserControl
{
    public string BoxInfo
    {
        get { return (string)GetValue(BoxInfoProperty); }
        set { SetValue(BoxInfoProperty, value); }
    }

    public static readonly StyledProperty<string> BoxInfoProperty =
        AvaloniaProperty.Register<UserBox, string>(nameof(BoxInfo), defaultValue: string.Empty, defaultBindingMode: Avalonia.Data.BindingMode.OneWay);

    public UserBox()
    {
        InitializeComponent();
        this.DataContext = this;
    }
}

and the axaml:

<UserControl xmlns="https://github.com/avaloniaui"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d" d:DesignWidth="400" d:DesignHeight="250"
             x:Class="MyAvaloniaTest.UserBox"
             x:CompileBindings="False">
    <Grid>
        <TextBlock Text="{Binding BoxInfo, Mode=OneWay}" />
    </Grid>
</UserControl>

Now, in main window I can use this control as follows:

<local:UserBox BoxInfo="Some text" />

This works.

I can give it a name:

<local:UserBox x:Name="MyBox" BoxInfo="Some text" />

and then use it in the code:

if (this.GetControl<UserBox>("MyBox") is UserBox ub) ub.BoxInfo = "Some text";

And this also works.

But then, when I define a property in the view model of main window:

private string _mainBoxMessage = string.Empty;

public string MainBoxMessage 
{
    get => this._mainBoxMessage ;
    set 
    { 
        this._mainBoxMessage = value; 
        OnPropertyChanged(nameof(MainBoxMessage ));
    }
}

Then in axaml of main window:

<local:UserBox x:Name="MyBox" BoxInfo="{Binding MainBoxMessage}" />

And in main window code:

if (this.DataContext is MainWindowViewModel dc) dc.MainBoxMessage = "Some bound text";

This doesn’t work.

I’ve tried setting x:CompileBindings to true, it didn’t help either.

What am I missing?

Btw., if I add another control, like this:

<TextBlock Text="{Binding MainBoxMessage}" />

its content is set according to the value of MainBoxMessage.

Similar to WPF, the Binding expression

BoxInfo="{Binding MainBoxMessage}" 

requires that the UserBox control inherits the value of its DataContext property from its parent element. This is however not happening because you are overriding the property value inheritance by explictly assigning the DataContext in the UserBox constructor. So just remove that assignment from the constructor.

In general, a control with bindable properties should never set its own DataContext – neither in WPF nor in Avalonia – because doing so breaks the standard data binding behaviour of the framework.

public UserBox()
{
    InitializeComponent();
    // DataContext = this; --- remove this line
}

In the UserControl’s XAML, Bindings of its own properties would now declare the UserControl as RelativeSource e.g. like

<TextBlock Text="{Binding BoxInfo,
    RelativeSource={RelativeSource AncestorType=UserControl}}" />

As a note, unlike in WPF you do not need to cast the return value of the GetValue method, so the property declaration would look like this:

public string BoxInfo
{
    get { return GetValue(BoxInfoProperty); } // no cast necessary
    set { SetValue(BoxInfoProperty, value); }
}

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật