WPF 用户与自定义控件

发布于 2024-11-14 03:42:41 字数 1640 浏览 0 评论 0原文

我有一个在整个应用程序中使用的网格控件。我想扩展网格控件以包含一个上下文菜单,其中包含一项“冻结/解冻列”。如果我选择使用自定义控件,则无法在控件内实现此功能 - 相反,我必须在放置自定义控件的任何位置实现该功能。另一种选择是用户控件,我可以在其中实现控件内的所有必要功能:

<Grid>
    <dxg:GridControl Name="gridData" DataSource="{Binding}" dx:DXSerializer.StoreLayoutMode="All">
        <dxg:GridControl.Resources></dxg:GridControl.Resources>
        <dxg:GridControl.Columns />
        <dxg:GridControl.View>
            <dxg:TableView ShowGroupPanel="False" MouseRightButtonUp="TableView_MouseRightButtonUp">
                <dxg:TableView.ColumnMenuCustomizations>
                    <dxb:BarButtonItem  Name="freezeColButton" Content="Freeze Column(s)" dxb:BarItemLinkActionBase.ItemLinkIndex="0" ItemClick="freezeColButton_ItemClick" />
                </dxg:TableView.ColumnMenuCustomizations>
            </dxg:TableView>
        </dxg:GridControl.View>
    </dxg:GridControl>
</Grid>

注意,TableView.ColumnMenuCustomization 标记包括用于冻结/解冻功能的事件处理程序。

但是,用户控件的唯一问题是我无法访问底层网格的 Columns 属性。例如,当我将用户控件(上面定义的)放置在窗口中时,出现错误(错误 25:XML 命名空间“clr-namespace:UI.Controls”中不存在标签“ExtendedGridControl.Columns”)

<Window>
    ...
     <Grid>
        <uc:ExtendedGridControl x:Name="extendedGridData" >
            <uc:ExtendedGridControl.Columns>
                <dxg::GridColumn FieldName="FieldA" Visible="True" />
                ...
            </uc:ExtendedGridControl.Columns>
        </uc:ExtendedGridControl>
    </Grid
</Window>

:我可以公开 GridControl 属性吗?任何帮助/建议将不胜感激。

I have a grid control that I use throughout the application. I would like to extend the grid control to include a context menu with one item "freeze/unfreeze columns". If I elect to use a custom control, I cannot implement this functionality within the control -- instead, I have to implement the functionality wherever I place my custom control. The other alternative is user control, in which I can implement all the necessary functionality within the control:

<Grid>
    <dxg:GridControl Name="gridData" DataSource="{Binding}" dx:DXSerializer.StoreLayoutMode="All">
        <dxg:GridControl.Resources></dxg:GridControl.Resources>
        <dxg:GridControl.Columns />
        <dxg:GridControl.View>
            <dxg:TableView ShowGroupPanel="False" MouseRightButtonUp="TableView_MouseRightButtonUp">
                <dxg:TableView.ColumnMenuCustomizations>
                    <dxb:BarButtonItem  Name="freezeColButton" Content="Freeze Column(s)" dxb:BarItemLinkActionBase.ItemLinkIndex="0" ItemClick="freezeColButton_ItemClick" />
                </dxg:TableView.ColumnMenuCustomizations>
            </dxg:TableView>
        </dxg:GridControl.View>
    </dxg:GridControl>
</Grid>

Notice, the TableView.ColumnMenuCustomization tag includes the event handler for the freeze/unfreeze functionality.

However, the only issue with the user control is that I cannot access the underlying Grid's Columns property. For example, when I place my user control (defined above) in a window, I get an error (Error 25: The tag 'ExtendedGridControl.Columns' does not exist in XML namespace 'clr-namespace:UI.Controls'):

<Window>
    ...
     <Grid>
        <uc:ExtendedGridControl x:Name="extendedGridData" >
            <uc:ExtendedGridControl.Columns>
                <dxg::GridColumn FieldName="FieldA" Visible="True" />
                ...
            </uc:ExtendedGridControl.Columns>
        </uc:ExtendedGridControl>
    </Grid
</Window>

How can I expose the GridControl properties? Any help/suggestions would be greatly appreciated.

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

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

发布评论

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

评论(1

念﹏祤嫣 2024-11-21 03:42:41

您需要通过在UserControl 上定义属性来传播属性,例如

public partial class Bogus : UserControl
{
    // You often can reuse properties via DependencyProperty.AddOwner
    public static readonly DependencyProperty ItemsSourceProperty = ItemsControl.ItemsSourceProperty.AddOwner(typeof(Bogus));
    public IEnumerable ItemsSource
    {
        get { return (IEnumerable)GetValue(ItemsSourceProperty); }
        set { SetValue(ItemsSourceProperty, value); }
    }

    public static readonly DependencyProperty ItemTemplateProperty = ItemsControl.ItemTemplateProperty.AddOwner(typeof(Bogus));
    public DataTemplate ItemTemplate
    {
        get { return (DataTemplate)GetValue(ItemTemplateProperty); }
        set { SetValue(ItemTemplateProperty, value); }
    }

    public Bogus()
    {
        InitializeComponent();
    }
}
<UserControl x:Class="Test.UserControls.Bogus" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" Name="control">
    <StackPanel>
        <TextBlock Text="Lorem Ipsum:" />
        <ItemsControl ItemsSource="{Binding ElementName=control, Path=ItemsSource}"
                ItemTemplate="{Binding ElementName=control, Path=ItemTemplate}" />
    </StackPanel>
</UserControl>

属性在外部可见,内部控件绑定到它们。

对于某些属性,您不使用 DependencyProperty,而仅使用引用内部控件属性的 clr-property,这对于仅具有 setter 或内部构造函数或也不是内部控件中的依赖属性的某些属性可能更可取,例如

public ItemCollection Items
{
    get { return _itemsControl.Items; }
}

You need to propagate the properties by defining them on the UserControl, e.g.

public partial class Bogus : UserControl
{
    // You often can reuse properties via DependencyProperty.AddOwner
    public static readonly DependencyProperty ItemsSourceProperty = ItemsControl.ItemsSourceProperty.AddOwner(typeof(Bogus));
    public IEnumerable ItemsSource
    {
        get { return (IEnumerable)GetValue(ItemsSourceProperty); }
        set { SetValue(ItemsSourceProperty, value); }
    }

    public static readonly DependencyProperty ItemTemplateProperty = ItemsControl.ItemTemplateProperty.AddOwner(typeof(Bogus));
    public DataTemplate ItemTemplate
    {
        get { return (DataTemplate)GetValue(ItemTemplateProperty); }
        set { SetValue(ItemTemplateProperty, value); }
    }

    public Bogus()
    {
        InitializeComponent();
    }
}
<UserControl x:Class="Test.UserControls.Bogus" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" Name="control">
    <StackPanel>
        <TextBlock Text="Lorem Ipsum:" />
        <ItemsControl ItemsSource="{Binding ElementName=control, Path=ItemsSource}"
                ItemTemplate="{Binding ElementName=control, Path=ItemTemplate}" />
    </StackPanel>
</UserControl>

The properties are visible outside and the internal controls bind to them.

For some properties you do not use a DependencyProperty, but just a clr-property which references the internal control's property, this may be preferable with certain properties that only have setters or internal constructors or are not dependency properties in the internal controls either, e.g.

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