设置数据模板的样式以适合 silverlight datgrid 列标题

发布于 2024-09-30 13:16:51 字数 1291 浏览 1 评论 0原文

我在 xaml 页面中有一个带 datatemplate 的样式,如下所示。

<

    Style x:Name="mytemplate" x:Key="mytemplate"  xmlns:dataprimitives="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Data"
                                TargetType="dataprimitives:DataGridColumnHeader">
                <Setter Property="ContentTemplate" >
                    <Setter.Value>
                        <DataTemplate>
                            <StackPanel Background="Aqua" Height="{Binding this.DataGridColumnHeader.Height}" Width="{Binding this.DataGridColumnHeaderWidth}" >
       <TextBlock Text="{Binding}"   HorizontalAlignment="Center" FontWeight="Black" ></TextBlock>
                                 <TextBox x:Name="{Binding}" Padding="0,-1,0,0"  HorizontalAlignment="Stretch" Width="100" Height="20" KeyDown="txtfilterBox_KeyDown" LostFocus="txtfilterBox_LostFocus" />
                            </StackPanel>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

此样式将应用于 silverlight datgrid 列标题样式。 现在我希望模板内的堆栈面板与模板的高度和宽度相同 silverlight datgrid 列标题?那么如何才能做到这一点呢?

否则如何拉伸内容模板以填充数据网格列标题的整个空间

i have a style with datatemplate in xaml page as shown below .

<

    Style x:Name="mytemplate" x:Key="mytemplate"  xmlns:dataprimitives="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Data"
                                TargetType="dataprimitives:DataGridColumnHeader">
                <Setter Property="ContentTemplate" >
                    <Setter.Value>
                        <DataTemplate>
                            <StackPanel Background="Aqua" Height="{Binding this.DataGridColumnHeader.Height}" Width="{Binding this.DataGridColumnHeaderWidth}" >
       <TextBlock Text="{Binding}"   HorizontalAlignment="Center" FontWeight="Black" ></TextBlock>
                                 <TextBox x:Name="{Binding}" Padding="0,-1,0,0"  HorizontalAlignment="Stretch" Width="100" Height="20" KeyDown="txtfilterBox_KeyDown" LostFocus="txtfilterBox_LostFocus" />
                            </StackPanel>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

this style would be applied to the silverlight datgrid column header style.
now i want the stackpanel inside the template to be same as the height and width of the
silverlight datgrid column header ? so how can that be done?

else how to stretch the content template to fill the whole space of the datagrid column header

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

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

发布评论

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

评论(1

面犯桃花 2024-10-07 13:16:51

您是否尝试过将 StackPanel 的水平和垂直对齐属性设置为拉伸?您根本不需要绑定到父容器的 Width 和 Height 属性。

如果将 StackPanel 设置为拉伸不起作用,只需将堆栈面板包裹在边框中即可。请参阅下面的代码:

<Style x:Name="mytemplate" x:Key="mytemplate"  xmlns:dataprimitives="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Data"
                                    TargetType="dataprimitives:DataGridColumnHeader">
                    <Setter Property="ContentTemplate" >
                        <Setter.Value>
                            <DataTemplate>
                    <Border Background="Aqua" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
                                    <StackPanel>
                          <TextBlock Text="{Binding}"   HorizontalAlignment="Center" FontWeight="Black" ></TextBlock>
                                      <TextBox x:Name="{Binding}" Padding="0,-1,0,0"  HorizontalAlignment="Stretch" Width="100" Height="20" KeyDown="txtfilterBox_KeyDown" LostFocus="txtfilterBox_LostFocus" />
                                    </StackPanel>
                    </Border>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>

Have you tried setting the StackPanel's horizontal and vertical alignment properties to stretch? You shouldn't need to bind to the Width and Height properties of the parent container at all.

If setting the StackPanel to stretch that doesn't work, simply wrap the stack panel in a Border and you will be golden. See the code below:

<Style x:Name="mytemplate" x:Key="mytemplate"  xmlns:dataprimitives="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Data"
                                    TargetType="dataprimitives:DataGridColumnHeader">
                    <Setter Property="ContentTemplate" >
                        <Setter.Value>
                            <DataTemplate>
                    <Border Background="Aqua" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
                                    <StackPanel>
                          <TextBlock Text="{Binding}"   HorizontalAlignment="Center" FontWeight="Black" ></TextBlock>
                                      <TextBox x:Name="{Binding}" Padding="0,-1,0,0"  HorizontalAlignment="Stretch" Width="100" Height="20" KeyDown="txtfilterBox_KeyDown" LostFocus="txtfilterBox_LostFocus" />
                                    </StackPanel>
                    </Border>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文