Help me please translate this Binding from XAML into the code: Width="{Binding Width, ElementName=dockPanel} .

My XAML:

 <ListBoxItem Content="Это строчка :D" FontWeight="Bold" Height="50" Width="{Binding Width, ElementName=dockPanel}"/> 

My code is:

 ListBoxItem myListBoxItem = new ListBoxItem(); myListBoxItem.FontWeight = System.Windows.FontWeights.Bold; myListBoxItem.Height = 50; myListBoxItem.Width = SetBinding(Width, dockPanel.Width); myListBoxItem.Content = "Моя строчка :D"; this.mylistbox.Items.Add(myListBoxItem); 

    1 answer 1

    Binding in the code is created like this:

     myListBoxItem.SetBinding( ListBoxItem.WidthProperty, new Binding("Width") { Source = this.dockPanel }); 

    But there is a chance that you do not really need it. It’s better not to create the items manually (we don’t have WinForms in the end), but to put the data you want to display into the ItemsSource and configure the ItemTemplate .

    • do not worry, everything works, thank you: D - alex-rudenkiy
    • @ alex-rudenkiy: That's good :) Please! - VladD