简体   繁体   中英

Use Binding for ElementName

I was unable to find a solution for this problem:

I am creating my own table with ItemControls. To resize the columns I'd like to bind the Width-property of one cell to the corresponding ActualWidth-property of the column.

I tried the following, but it is not working:

<StackPanel Orientation="Vertical">

    <Border BorderBrush="Black" BorderThickness="1">
        <ItemsControl ItemsSource="{Binding Data.Columns}" Margin="26,1,1,1">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <!-- here i set the name of the column (Code) -->
                    <Border BorderBrush="Black" BorderThickness="1,0,0,0" Width="100" x:Name="{Binding Code}">
                        <TextBlock Text="{Binding Header}" />
                    </Border>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Border>


    <ItemsControl ItemsSource="{Binding Data.Rows}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Vertical" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Border BorderBrush="Black" BorderThickness="1,0,1,1">

                    <StackPanel Orientation="Horizontal" Margin="1">

                        <Grid Width="25">
                            <General:SeverityIconControl Severity="{Binding Severity}" />
                        </Grid>

                        <ItemsControl ItemsSource="{Binding Values}">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <StackPanel Orientation="Horizontal"/>
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <!-- and here i'd like to bind to the ActualWidth of the corresponding column (Code)
                                    but it is not working because ElementName={Binding LocalCode} does not work -->
                                    <Border BorderBrush="Black" BorderThickness="1,0,0,0" Width="{Binding ActualWidth, ElementName={Binding LocalCode}}">
                                        <TextBlock Text="{Binding Value}" />
                                    </Border>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </StackPanel>
                </Border>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</StackPanel>

Is there a way to solve this without changing the data source (unfortunately the data source cannot be edited)?

You can't bind x:Name (more info here ) since it's only read when you call InitializeComponent() in the constructor.

Easiest way would be to add a DesiredWidth column to your Data object, and bind both widths to that (with a default value of 100).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM