WPF控件,UserControl,模板混淆

时间:2014-02-11 19:16:34

标签: wpf xaml

披露:

我是WPF的新手,大约一个星期。

问题:

我正在尝试修改GridSplitter的行为,使其捕捉到有趣的位置,显示带有当前位置的标签(在分割器后面),从所述标签驱动上下文菜单等。我有在一个简单的测试应用程序中,在一个gridsplitter上成功地完成了所有这些操作,并结合了XAML和一些代码。

值得注意的是,由于GridSplitter无法托管内容,因此我将标签放在与分割器相同的网格单元格中,以便它们一起移动。

到目前为止一直很好......

现在我希望复制我的工作,以便我可以在许多地方使用我的新GridSplitter功能代替原生控件,而且,我希望有两个变体,水平和垂直。听起来像继承...创建一个派生自GridSplitter的子类并添加其他功能。但是我所做的所有阅读都让我想知道如何解决这个问题,如果没有重新开始并且从头开始构建我自己的GridSplitter,这是否可行?

欢迎提示。在那之前,我将恢复胎位。

由于

3 个答案:

答案 0 :(得分:1)

此答案可能有助于您解决问题:How to make GridSplitter to "snap" into another element?

通过订阅GridSplitterDragCompleted事件,您可以插入逻辑以捕捉到“有趣”的位置。

答案 1 :(得分:0)

你应该

  1. 创建一个从GridSplitter派生的新控件。
  2. 订阅DragCompleted事件以实现像DLeh所述的捕捉功能。
  3. 为Label,ContextMenu等添加一些新属性。
  4. 为您的新控件提供样式。

答案 2 :(得分:0)

这回答了如何在分割器中放置内容

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="40" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions> 
    <Button Grid.Row="0" Content="Row 0" Background="Orange"/>
    <!--<GridSplitter Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Stretch" Height="20" Background="Purple"/>-->
    <GridSplitter Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Stretch">
        <GridSplitter.Template>
            <ControlTemplate TargetType="{x:Type GridSplitter}">
                <TextBlock Text="TextBlock splitter" Background="Yellow" FontWeight="Bold"/>
            </ControlTemplate>
        </GridSplitter.Template>
    </GridSplitter>
    <Button Grid.Row="2" Content="Row 0" Background="Salmon"/>
</Grid>
相关问题