如何绑定与模板化的 GridViewColumn 相同的用户控件宽度?

问题描述

我有一个名为 SegmentedProgressBar用户控件,它的子控件为 StackPanel。我想在 ListView 列中使用我的用户控件,并且在调整列大小时,我也希望 StackPanel 也调整大小。我尝试了下面的代码,但是当列调整大小时它不会调整大小。我该如何解决这个问题?

SegmentedProgressBar

<UserControl x:Class="WpfApplication1.SegmentedProgressBar"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="20" d:DesignWidth="300" HorizontalAlignment="Stretch" x:Name="ucsg">
<StackPanel x:Name="spanel" Orientation="Horizontal" Background="Blue" HorizontalAlignment="Stretch">
    
</StackPanel>

主窗口

<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<Grid>
    <StackPanel>
        <ListView Name="listview1">
            <ListView.View>
                <GridView>
                     <GridViewColumn x:Name="SPBarCell"  Width="100" Header="Segmented" >
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <local:SegmentedProgressBar x:Name="test" Width="{Binding Path=Width,ElementName=SPBarCell}" Height="20" Margin="0"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>

                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
    </StackPanel>
</Grid>

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)