从CustomControl WPF中定义的Datagrid访问Selecteditem(Access Selecteditem from Datagrid defined inside Custom

编程入门 行业动态 更新时间:2024-10-23 23:28:28
从CustomControl WPF中定义的Datagrid访问Selecteditem(Access Selecteditem from Datagrid defined inside CustomControl WPF)

我已经定义了一个名为MainView的CustomControl,如下所示:

public class MainView : ContentControl { static MainView() { DefaultStyleKeyProperty.OverrideMetadata(typeof(MainView), new FrameworkPropertyMetadata(typeof(MainView))); } public virtual void Show(MyClass obj) { this.DataContext = SomeMethodToGetListOfMyClass(); } }

这是上面CustomControl的Generic.xaml代码

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:OneTouchCustomControlLib"> <Style TargetType="{x:Type local:MainView}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type local:MainView}"> <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> <Grid Name="Maingrid"> <DataGrid Name="dtMainView" Margin="10,10,10,10" RowHeight="20" AutoGenerateColumns="True" ItemsSource="{Binding}" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible" Height="auto" Width="auto"> </DataGrid> </Grid> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> </ResourceDictionary>

在另一个项目中,我使用了这个CustomControl在stackpanel中显示数据网格,并且成功地能够这样做。

现在,我想要的是,点击一个按钮,我想从datagrid访问所选项目/索引并进行进一步处理。 我想我错过了一些东西。 任何人都可以帮忙吗??

提前致谢。

I have defined a CustomControl named MainView as follows :

public class MainView : ContentControl { static MainView() { DefaultStyleKeyProperty.OverrideMetadata(typeof(MainView), new FrameworkPropertyMetadata(typeof(MainView))); } public virtual void Show(MyClass obj) { this.DataContext = SomeMethodToGetListOfMyClass(); } }

This is the Generic.xaml code for the above CustomControl

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:OneTouchCustomControlLib"> <Style TargetType="{x:Type local:MainView}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type local:MainView}"> <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> <Grid Name="Maingrid"> <DataGrid Name="dtMainView" Margin="10,10,10,10" RowHeight="20" AutoGenerateColumns="True" ItemsSource="{Binding}" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible" Height="auto" Width="auto"> </DataGrid> </Grid> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> </ResourceDictionary>

In another project, I have used this CustomControl to show a datagrid inside a stackpanel and successfully able to do so.

Now, what I Want is, On click of a button , I want to access selected item/index from datagrid and do further processing. I think I have missed out something. Can anyone help to do so.. ?

Thanks in advance.

最满意答案

我使用GetTemplateChild方法获得了解决方案。 另外,从Control类继承了我的CustomControl。

这是新的CustomControl定义,

public class MainView : Control { static MainView() { DefaultStyleKeyProperty.OverrideMetadata(typeof(MainView), new FrameworkPropertyMetadata(typeof(MainView))); } public virtual void Show(MyClass obj) { this.DataContext = SomeMethodToGetListOfMyClass(); } **public void GetDatagrid() { return GetTemplateChild("dtMainView") as DataGrid; }** }

I got the solution using GetTemplateChild Method. Also, inherited my CustomControl from Control class.

Here's new CustomControl definition,

public class MainView : Control { static MainView() { DefaultStyleKeyProperty.OverrideMetadata(typeof(MainView), new FrameworkPropertyMetadata(typeof(MainView))); } public virtual void Show(MyClass obj) { this.DataContext = SomeMethodToGetListOfMyClass(); } **public void GetDatagrid() { return GetTemplateChild("dtMainView") as DataGrid; }** }

更多推荐

本文发布于:2023-07-19 09:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1176924.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:定义   WPF   CustomControl   Datagrid   defined

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!