绑定到自定义ControlTemplate中的属性

时间:2011-09-08 17:33:24

标签: wpf binding properties controltemplate

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

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

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

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

    <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>

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

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