开发者

WPF Binding a Dynamically Added Control

开发者 https://www.devze.com 2023-01-25 00:10 出处:网络
I am adding a to the \"Company\" RibbonApplicationMenuItem in my RibbonWindow with the following code:

I am adding a to the "Company" RibbonApplicationMenuItem in my RibbonWindow with the following code:

  var reset = DataContext as ICompanies;
  if (reset != null)
  {

    // ToDo: Create interface to populate the mymenutems
    var mymenuitems = new List<string>();
    foreach (var item in mymenuitems)
    {
      var newbutton = new Button { Margin = new Thickness(2), Content = item };
      MenuItem_Company.Items.Add(newbutton);
    }
  }
开发者_StackOverflow中文版

My XAML looks like this:

<ribbon:RibbonApplicationMenu ToolTipTitle="Application Menu">
    <ribbon:RibbonApplicationMenuItem 
        Header="Company"
        x:Name="MenuItem_Company"
        ImageSource="Images\LargeIcon.png">                     
    </ribbon:RibbonApplicationMenuItem>
</ribbon:RibbonApplicationMenu>

How do I bind my new button in code when I add it to the MenuItem_Company? I need it to bind to a property in my datacontext.

Thanks, Eroc


var newbutton = new Button { Margin = new Thickness(2), Content = item };
Binding b = new Binding();
b.Source = reset;
b.Path = new PropertyPath("SomePropertyOnDataContext");
newButton.SetBinding(Button.IsEnabledProperty, b);

Varying assumptions in the code...but it should give you an idea where to start...

0

精彩评论

暂无评论...
验证码 换一张
取 消