动态网格视图列

时间:2017-06-30 16:11:20

标签: c# wpf

我的表目前有一个列,用户可以在其中添加或删除对象。我想知道实现这个的最佳方法是什么。目前我正在使用ListBox,但我在如何以编程方式删除项目时遇到麻烦,打开项目似乎也很糟糕,我必须通过可视树直到找到对象发送者。

<DataTemplate x:Key="ItemTemplate">
    <StackPanel Orientation="Horizontal">
        <ListBox MinWidth="0" 
                                        ItemsSource="{Binding SelectedAttachments}" 
                                        DisplayMemberPath="FileName">
            <ListBox.Resources>
                <Style x:Key="DeleteItemContextMenuStyle" TargetType="MenuItem">
                    <EventSetter Event="Click" Handler="DeleteEvent"/>
                </Style>
                <Style x:Key="OpenItemContextMenuStyle" TargetType="MenuItem">
                    <EventSetter Event="Click" Handler="MenuOpenEvent"/>
                </Style>
            </ListBox.Resources>
            <ListBox.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}">
                    <EventSetter Event="MouseDoubleClick" Handler="OpenEvent"></EventSetter>
                    <Setter Property="ContextMenu">
                        <Setter.Value>
                            <ContextMenu>
                                <MenuItem Header="Open" Style="{StaticResource OpenItemContextMenuStyle}"/>
                                <MenuItem Header="Delete" Style="{StaticResource DeleteItemContextMenuStyle}"/>
                            </ContextMenu>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ListBox.ItemContainerStyle>
        </ListBox>
        <Button Foreground="DeepSkyBlue" Height="20" Click="ButtonBase_OnClick"  cal:Message.Attach ="[Event Click] = [AttachFileToRisk]" FontWeight="Bold">Add File</Button>
    </StackPanel>
</DataTemplate>

<telerik:RadGridView ShowGroupPanel="False" RowIndicatorVisibility="Collapsed" ItemsSource="{Binding SelectedRiskBatch.Entries}" IsReadOnly="False" SelectedItem="{Binding SelectedEntry}">
    <telerik:RadGridView.Columns>
         <telerik:GridViewDataColumn Header="Attachments" CellTemplate="{StaticResource ItemTemplate}" CellEditTemplate="{StaticResource ItemTemplate}"/>
    </telerik:RadGridView.Columns>
</telerik:RadGridView>

代码背后

private void MenuOpenEvent(object sender, RoutedEventArgs e)
{
    var s = e.OriginalSource as FrameworkElement;

    var parentRow = s.ParentOfType<GridViewRow>();

    if (parentRow != null)
    {
        parentRow.IsSelected = true;
    }

    DependencyObject dep = (DependencyObject)e.OriginalSource;
    DependencyObject dep1 = VisualTreeHelper.GetParent(dep);
    DependencyObject dep2 = VisualTreeHelper.GetParent(dep1);
    DependencyObject dep3 = VisualTreeHelper.GetParent(dep2);
    DependencyObject dep4 = VisualTreeHelper.GetParent(dep3);
    DependencyObject dep5 = VisualTreeHelper.GetParent(dep4);
    DependencyObject dep6 = VisualTreeHelper.GetParent(dep5);
    DependencyObject dep7 = VisualTreeHelper.GetParent(dep6);
    DependencyObject dep8 = VisualTreeHelper.GetParent(dep7); 
    DependencyObject dep9 = VisualTreeHelper.GetParent(dep8);
    DependencyObject dep10 = VisualTreeHelper.GetParent(dep9);

    var contextMenu = (ContextMenu)dep10;
    var listboxitem = (ListBoxItem) contextMenu.PlacementTarget;
    var file = (File)listboxitem.Content;

    var vm = (BusinessViewModel)DataContext;
    vm.OpenRiskFile(file);
}

请问如何实现删除事件,或者我是否可以获得更好的方法来实现动态列的建议,其中可以添加/删除对象。

0 个答案:

没有答案