我已经定义了一个名为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; }** }更多推荐
发布评论