【问题标题】:Convert XAML Binding to C#将 XAML 绑定转换为 C#
【发布时间】:2012-03-21 16:51:01
【问题描述】:

我正在构建一些动态控件,并希望将以下 XAML 转换为 C#...

Height="{Binding ElementName=GridGroup1, Path=ActualHeight}">

以上内容是我想设置的动态构建的边框控件的一部分,以便它与动态构建的网格具有相同的高度。

请告诉我如何做到这一点?我发现的所有例子似乎都不完整。

谢谢,

保罗。

修正...好的,这里是完整的 XAML...

<Grid x:Name="GridGroup1" HorizontalAlignment="Left"
                  Margin="20,14,0,0"
                  Width="250"
                  VerticalAlignment="Top">
                <Border BorderThickness="1"
                        CornerRadius="5"
                        Background="{StaticResource PanelBackground}"
                        BorderBrush="{StaticResource PanelBorderBrush}"
                        HorizontalAlignment="Left"
                        VerticalAlignment="Top"
                        Margin="0,8,0,0"
                        Width="250"
                        Height="{Binding ElementName=GridGroup1, Path=ActualHeight}">
                    <Border.Effect>
                        <DropShadowEffect />
                    </Border.Effect>
                </Border>

【问题讨论】:

  • 如果没有看到 XAML 的其余部分,就无法回答这个问题——至少对于有问题的控件。

标签: c# xaml binding code-behind


【解决方案1】:

类似:

    Binding binding = new Binding();
    binding.Source = GridGroup1;
    binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
    binding.Path = new PropertyPath("ActualHeight");
    MyGridBorder.SetBinding(Border.HeightProperty, binding);

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-19
相关资源
最近更新 更多