绑定到自定义 ControlTemplate 中的属性

发布于 2024-12-03 21:32:47 字数 5687 浏览 0 评论 0原文

我有一个在单独的资源文件中定义的自定义数据样式网格。在这种风格中,我重写 ControlTemplate 以更改其外观。

然后我有自己的 CustomDataGrid 控件,该控件派生自 UserControl 并使用资源文件中的样式。

我需要能够让垂直滚动条具有用户定义的宽度。我知道如何通过硬编码值来更改滚动条宽度。我不能做的是在我的自定义控件中我可以设置滚动条宽度。

这是我的模板样式在资源文件中的样子,其中我需要滚动条宽度,我有一系列问号。

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGrid}">
                <Border x:Name="border" SnapsToDevicePixels="True" BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}" Background="{TemplateBinding RowBackground}">
                    <ScrollViewer x:Name="DG_ScrollViewer" Focusable="false" Background="Black">
                        <ScrollViewer.Template>
                            <ControlTemplate TargetType="{x:Type ScrollViewer}">
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto" />
                                        <ColumnDefinition Width="*" />
                                        <ColumnDefinition Width="Auto" />
                                    </Grid.ColumnDefinitions>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto" />
                                        <RowDefinition Height="*" />
                                        <RowDefinition Height="Auto" />
                                    </Grid.RowDefinitions>
                                    <Button Focusable="false" Command="{x:Static DataGrid.SelectAllCommand}"
                                        Style="{DynamicResource {ComponentResourceKey 
                                        ResourceId=DataGridSelectAllButtonStyle, 
                                        TypeInTargetAssembly={x:Type DataGrid}}}"
                                        Visibility="{Binding HeadersVisibility, 
                                        ConverterParameter={x:Static DataGridHeadersVisibility.All}, 
                                        Converter={x:Static DataGrid.HeadersVisibilityConverter}, 
                                        RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
                                        Width="{Binding CellsPanelHorizontalOffset, 
                                        RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
                                    <DataGridColumnHeadersPresenter x:Name="PART_ColumnHeadersPresenter" Grid.Column="1" Height="32"
                                        Visibility="{Binding HeadersVisibility, 
                                        ConverterParameter={x:Static DataGridHeadersVisibility.Column}, 
                                        Converter={x:Static DataGrid.HeadersVisibilityConverter}, 
                                        RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
                                    <ScrollContentPresenter x:Name="PART_ScrollContentPresenter" Grid.ColumnSpan="2"
                                        Grid.Row="1" CanContentScroll="{TemplateBinding CanContentScroll}" />
                                    <ScrollBar x:Name="PART_VerticalScrollBar" Grid.Column="2" Grid.Row="0" Grid.RowSpan="2"
                                        Orientation="Vertical" ViewportSize="{TemplateBinding ViewportHeight}"
                                        Maximum="{TemplateBinding ScrollableHeight}"
                                        Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"
                                        Width="{????????????????????????}"       
                                        Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"/>
                                    <Grid Grid.Column="1" Grid.Row="2">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="{Binding NonFrozenColumnsViewportHorizontalOffset, 
                                                RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
                                            <ColumnDefinition Width="*" />
                                        </Grid.ColumnDefinitions>
                                        <ScrollBar x:Name="PART_HorizontalScrollBar" Grid.Column="1" Orientation="Horizontal"
                                            ViewportSize="{TemplateBinding ViewportWidth}" Maximum="{TemplateBinding ScrollableWidth}"
                                            Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"
                                            Value="{Binding HorizontalOffset, Mode=OneWay, 
                                            RelativeSource={RelativeSource TemplatedParent}}"/>
                                        </Grid>
                                    </Grid>
                            </ControlTemplate>
                        </ScrollViewer.Template>
                        <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    </ScrollViewer>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

有人可以帮我吗?

I have a custom data style grid that I have defined in a seperate resource file. In this style I override the ControlTemplate to change it's appearance.

Then I have my own CustomDataGrid control that is derived from UserControl and uses the style in the resource file.

I need to be able to have the vertical scroll bars have a user defined width. I know how to change the scroll bar width by hard coding a value. What I cannot do is make is so that in my custom control I can set the scroll bar width.

Here is what my Template style looks like in the resource file, where I need the scroll bar width I have a series of question marks.

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGrid}">
                <Border x:Name="border" SnapsToDevicePixels="True" BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}" Background="{TemplateBinding RowBackground}">
                    <ScrollViewer x:Name="DG_ScrollViewer" Focusable="false" Background="Black">
                        <ScrollViewer.Template>
                            <ControlTemplate TargetType="{x:Type ScrollViewer}">
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto" />
                                        <ColumnDefinition Width="*" />
                                        <ColumnDefinition Width="Auto" />
                                    </Grid.ColumnDefinitions>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto" />
                                        <RowDefinition Height="*" />
                                        <RowDefinition Height="Auto" />
                                    </Grid.RowDefinitions>
                                    <Button Focusable="false" Command="{x:Static DataGrid.SelectAllCommand}"
                                        Style="{DynamicResource {ComponentResourceKey 
                                        ResourceId=DataGridSelectAllButtonStyle, 
                                        TypeInTargetAssembly={x:Type DataGrid}}}"
                                        Visibility="{Binding HeadersVisibility, 
                                        ConverterParameter={x:Static DataGridHeadersVisibility.All}, 
                                        Converter={x:Static DataGrid.HeadersVisibilityConverter}, 
                                        RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
                                        Width="{Binding CellsPanelHorizontalOffset, 
                                        RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
                                    <DataGridColumnHeadersPresenter x:Name="PART_ColumnHeadersPresenter" Grid.Column="1" Height="32"
                                        Visibility="{Binding HeadersVisibility, 
                                        ConverterParameter={x:Static DataGridHeadersVisibility.Column}, 
                                        Converter={x:Static DataGrid.HeadersVisibilityConverter}, 
                                        RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
                                    <ScrollContentPresenter x:Name="PART_ScrollContentPresenter" Grid.ColumnSpan="2"
                                        Grid.Row="1" CanContentScroll="{TemplateBinding CanContentScroll}" />
                                    <ScrollBar x:Name="PART_VerticalScrollBar" Grid.Column="2" Grid.Row="0" Grid.RowSpan="2"
                                        Orientation="Vertical" ViewportSize="{TemplateBinding ViewportHeight}"
                                        Maximum="{TemplateBinding ScrollableHeight}"
                                        Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"
                                        Width="{????????????????????????}"       
                                        Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"/>
                                    <Grid Grid.Column="1" Grid.Row="2">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="{Binding NonFrozenColumnsViewportHorizontalOffset, 
                                                RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
                                            <ColumnDefinition Width="*" />
                                        </Grid.ColumnDefinitions>
                                        <ScrollBar x:Name="PART_HorizontalScrollBar" Grid.Column="1" Orientation="Horizontal"
                                            ViewportSize="{TemplateBinding ViewportWidth}" Maximum="{TemplateBinding ScrollableWidth}"
                                            Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"
                                            Value="{Binding HorizontalOffset, Mode=OneWay, 
                                            RelativeSource={RelativeSource TemplatedParent}}"/>
                                        </Grid>
                                    </Grid>
                            </ControlTemplate>
                        </ScrollViewer.Template>
                        <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    </ScrollViewer>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

Can anyone please help me?

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

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

发布评论

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

评论(1

零崎曲识 2024-12-10 21:32:47

创建一个名为(例如)ScrollBarWidth 的依赖属性,然后在模板中写入Width={TemplateBinding ScrollBarWidth}

Create a dependency property named (eg) ScrollBarWidth, then write Width={TemplateBinding ScrollBarWidth} in the template.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文