为ListView创建UserControl WPF按钮

时间:2015-05-31 07:57:34

标签: c# wpf xaml button

我需要列表视图中的按钮帮助。我正在为SCSM创建自定义表单。列表视图工作没有问题,但我需要添加添加按钮,打开按钮和删除按钮。添加按钮工作没有问题,但其他按钮什么都不做。任何人都可以帮助我吗?

由于

XAML代码在这里:

<ListView Name="ListViewMonitor" VerticalAlignment="Top" HorizontalAlignment="Stretch" Margin="10,0,00,0" MinHeight="100" Grid.Column="0" Grid.Row="1" ItemsSource="{Binding Path=CustomRelatedMonitor, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
                        <ListView.View>
                            <GridView>
                                    <GridViewColumn Header="{Binding Path=Strings[ID].Value, FallbackValue=ID, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type views:FormView}}}" DisplayMemberBinding="{Binding Path=ID}" Width="Auto"/>
                                    <GridViewColumn Header="{Binding Path=Strings[CustomAssetStatus].Value, FallbackValue=CustomAssetStatus, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type views:FormView}}}" Width="Auto">
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                                <smcontrols:ListPicker Name="listPickerAssetStatus" HorizontalAlignment="Stretch" Width="Auto" ParentCategoryId="{Binding Source=12435943-EBA1-3E78-BB6C-A731E44EDCFD, Mode=OneWay}" SelectedItem="{Binding Path=CustomAssetStatus, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>

                                </GridView>
                        </ListView.View>
                    </ListView>
                        <StackPanel Margin="10,0,10,0" Grid.Row="1" Grid.Column="1" VerticalAlignment="Top" HorizontalAlignment="Right">
                            <Button  VerticalAlignment="Top" HorizontalAlignment="Right" Name="buttonAddMonitor"  Height="20" Width="65" Content="{Binding Path=Strings[buttonAdd].value, FallbackValue=Add, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type views:FormView}}}" Click="buttonAddMonitor_Click" />
                            <Button  VerticalAlignment="Top" HorizontalAlignment="Right" Margin="0,10,0,0" Name="buttonRemoveMonitor"  Height="20" Width="65" IsEnabled="{Binding ElementName=ListViewMonitor, Path=SelectedItems.Count}" Content="{Binding Path=Strings[buttonRemove].value, FallbackValue=Remove, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type views:FormView}}}" Click="buttonRemoveMonitor_Click" />
                            <Button  VerticalAlignment="Top" HorizontalAlignment="Right" Margin="0,10,0,0" Name="buttonOpenMonitor"  Height="20" Width="65" IsEnabled="{Binding ElementName=ListViewMonitor, Path=SelectedItems.Count}" Content="{Binding Path=Strings[buttonOpen].value, FallbackValue=Open, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type views:FormView}}}" Click="buttonOpenMonitor_Click" />
                        </StackPanel>

按钮代码:

public ListView ListViewAllMonitors { get; set; }
    private ListView GetListView (string Name)
    {
        switch (Name)
        {
            case "buttonAddMonitor":
            case "buttonRemoveMonitor":
            case "buttonOpenMonitor":
                {
                    return this.ListViewAllMonitors;
                }
            default:
                {
                    throw new Exception(String.Format("ListView for control: {0}- not found", Name));
                }
        }
    }



    private void buttonAddMonitor_Click(object sender, RoutedEventArgs e)
    {
        ButtonAdd_Click(new Guid("DD3BDB96-BC9E-D46E-5847-BAB387039B36"), ListViewMonitor);
    }

    private void ButtonAdd_Click(Guid mpClassGuid, ItemsControl listView)
    {
        FormUtilities.Instance.LaunchAddInstancePickerDialog(listView.ItemsSource as Collection<IDataItem>, mpClassGuid);
    }



    private void buttonRemoveMonitor_Click(object sender, RoutedEventArgs e)
    {
        var button = (Button)sender;
        var CustomListView = GetListView(button.Name);
        if (CustomListView == null) return;
        var list = (IList<IDataItem>)CustomListView.ItemsSource;
        if (list == null) return;
        foreach (IDataItem item in new ArrayList(CustomListView.SelectedItems))
        {
            list.Remove(item);
        }
    }

    private void buttonOpenMonitor_Click(object sender, RoutedEventArgs e)
    {
        var button = (Button)sender;
        var ListView = GetListView(button.Name);
        if (ListView == null) return;
        OpenInstance(ListView);
    }

    public void OpenInstance(ListView ListView)
    {
        if (ListView == null) return;
        foreach (IDataItem dataItem in ListView.SelectedItems)
        {
            FormUtilities.Instance.PopoutForm(dataItem);
        }

    }

0 个答案:

没有答案