WPF 绑定问题

发布于 2024-08-17 03:38:43 字数 2211 浏览 2 评论 0原文

我正在尝试自定义 TreeView 控件。当用户在 TreeView 中选择一个项目时,我需要将 SelectedItemActualWidth 存储在该项目的标签中:

 <Style x:Key="{x:Type TreeViewItem}" TargetType="{x:Type TreeViewItem}">
           <!-- ... -->
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TreeViewItem}">
                        <Grid ShowGridLines="True">

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

                            <Rectangle x:Name="rect" />

                            <ContentPresenter x:Name="PART_Header" ContentSource="Header" Margin="5" />         

                            <ItemsPresenter x:Name="ItemsHost" Grid.Row="1" Grid.Column="1" />
                            <!-- ... -->       
                        </Grid>

                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="true">
                                <Setter Property="Tag" Value="{Binding ElementName=rect, Path=ActualWidth}" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

稍后,我听到 TreeViewSelectedItemChanged 事件:

private void views_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
    {
        TreeViewItem item = (TreeViewItem)e.NewValue;
        double i = (double)item.Tag;
    }

现在的问题是 item.Tag 始终为 null。这是我绑定的问题吗?或者应该以不同的方式做事?

I'm trying to customize the TreeView control. When a user selects an item in the TreeView, I need the ActualWidth of the SelectedItem to be stored in the item's Tag:

 <Style x:Key="{x:Type TreeViewItem}" TargetType="{x:Type TreeViewItem}">
           <!-- ... -->
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TreeViewItem}">
                        <Grid ShowGridLines="True">

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

                            <Rectangle x:Name="rect" />

                            <ContentPresenter x:Name="PART_Header" ContentSource="Header" Margin="5" />         

                            <ItemsPresenter x:Name="ItemsHost" Grid.Row="1" Grid.Column="1" />
                            <!-- ... -->       
                        </Grid>

                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="true">
                                <Setter Property="Tag" Value="{Binding ElementName=rect, Path=ActualWidth}" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

Later, I listen to the SelectedItemChanged event of the TreeView:

private void views_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
    {
        TreeViewItem item = (TreeViewItem)e.NewValue;
        double i = (double)item.Tag;
    }

Now the problem is that item.Tag is always null. Is this a problem with my binding? Or should things be done in a different way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坚持沉默 2024-08-24 03:38:43

尝试一下:

<Setter Property="Tag" Value="{Binding Path=ActualWidth, RelativeSource={x:Static RelativeSource.Self}}" />

Try that :

<Setter Property="Tag" Value="{Binding Path=ActualWidth, RelativeSource={x:Static RelativeSource.Self}}" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文