how to bind width of child element to width of parent element in silverlight

后端 未结 6 1287
南笙
南笙 2021-01-30 20:15

I have a grid whose width is \"1*\". So the actual width decided at runtime I think. Within that grid I have another grid whose width I want to set to the runtime width of paren

6条回答
  •  长情又很酷
    2021-01-30 20:47

    If you are doing it in CodeBehind, this works for me. It has the added advantage that bindMe does not have to be a child of toMe:

    public static void BindWidth(this FrameworkElement bindMe, FrameworkElement toMe)
    {
      Binding b = new Binding();
      b.Mode = BindingMode.OneWay;
      b.Source = toMe.ActualWidth;
      bindMe.SetBinding(FrameworkElement.WidthProperty, b);
    }
    

    usage:

    child.BindWidth(parent);
    

提交回复
热议问题