WPF元素绑定与动态生成的UI元素

时间:2010-09-03 12:52:46

标签: wpf dynamic binding element

我有一个页面,其中我有一个有2列的网格,一个容纳80 *宽度,另一个容纳20 *。网格下面是一个堆栈面板,我在运行时加载UI元素(子堆栈面板)。

我的目标是将stackpanels的宽度绑定到网格的列。如果动态内容在设计视图中被模拟为静态,则此工作完美且stackpenels会调整大小。但是当运行应用程序时,绑定会失败并且宽度不会被绑定。

有没有办法刷新绑定,以便我可以通知stackpanels引用网格的宽度并配置它的大小?

更新: 这就是我的XAML的样子:

<Page x:Class="WPFTestApp.Page1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Page1">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <Grid x:Name="resizeGrid"
              Grid.Column="0" Grid.Row="0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition x:Name="leftColumn"
                                  Width="80*" />
                <ColumnDefinition x:Name="rightColumn"
                                  Width="20*" />
            </Grid.ColumnDefinitions>            
        </Grid>

        <StackPanel x:Name="contentPanel"
                    Grid.Column="0" Grid.Row="0" >
            <!-- Dynamic StackPanels are added here -->
        </StackPanel>
    </Grid>
</Page>

在我的代码中,我创建了StackPanel元素并将其添加到contentPanel,方法是: contentPanel.Children.Add(...);

不幸的是,我必须在这里使用StackPanels。 : - (

动态创建的StackPanel元素的标记如下(注意我使用绑定到XAML中已存在的网格):

<StackPanel x:Name="element01"
             Orientation="Horizontal"
             Width="{Binding ElementName=resizeGrid, Path=ActualWidth}" >
    <StackPanel x:Name="leftPanel"
                Orientation="Vertical"
                Width="{Binding ElementName=leftColumn, Path=ActualWidth}">
        <!-- Content goes here -->
    </StackPanel>
    <StackPanel x:Name="rightPanel"
                Orientation="Vertical"
                Width="{Binding ElementName=rightColumn, Path=ActualWidth}">
        <!-- Content goes here -->
    </StackPanel>
</StackPanel>

我的动态StackPanel元素的XAML代码是通过XSLT转换生成的

请注意xmlns:x命名空间动态StackPanel。这也必须完成,因为它是通过XSLT生成的

3 个答案:

答案 0 :(得分:0)

据我所知,设置Width的{​​{1}}与StackPanel无效。 Orientation="Horizontal"的大小不会超过其内容方向的内容大小。

为什么你“有”在这里使用StackPanel?鉴于你的目标,你几乎肯定想要StackPanel s。您不必绑定或设置任何内容,因为DockPanel将自动填充单元格。

答案 1 :(得分:0)

尝试绑定ActualWidth而不是Width。

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    <Grid x:Name="resizeGrid"
          Grid.Column="0" Grid.Row="0">
        <Grid.ColumnDefinitions>
            <ColumnDefinition x:Name="leftColumn"
                              Width="{Binding ElementName=contentPanel, Path=ActualWidth}" />
            <ColumnDefinition x:Name="rightColumn"
                              Width="*" />
        </Grid.ColumnDefinitions>
    </Grid>       


    <StackPanel x:Name="contentPanel"
                Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left" >
        <StackPanel Background="Yellow" Width="100" Height="25" />
        <StackPanel Background="Green" Width="120" Height="25" />
        <StackPanel Background="Red" Width="140" Height="25" />
        <TextBlock Background="Gray" Text="{Binding ElementName=contentPanel, Path=ActualWidth}" />
    </StackPanel>
</Grid>

我在上面做了以下更改:
1)将leftColumn设置为ActualWidth的{​​{1}} 2)将contentPanel设置为*
3)rightColumn Horizo​​ntalAlignment应在contentPanel之后更改。在这里,我将其更改为Stretch

HTH

答案 2 :(得分:0)

我找到了解决此问题的方法。 (对不起,迟到的回复)

我做的是,我将“resizeGrid”网格控件剪切并粘贴到“contentPanel”Stackpanel中。

正如我上面提到的,contentPanel中的内容是通过XSLT转换生成的,这肯定会产生命名空间引用问题。

,无论何时加载此窗口,我都会在“输出”窗口中注意到“找不到元素...”消息

将“resizeGrid”插入“contentPanel”会有所帮助,因为这两个控件都位于同一名称空间中,并且可以使用“ElementName”属性为PropertyBinding引用。

感谢您的支持! : - )

相关问题