WPF Datagrid不能通过单击鼠标选择行 [英] WPF Datagrid cannot select rows with mouse click

查看:115
本文介绍了WPF Datagrid不能通过单击鼠标选择行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带选项卡控件的WPF应用程序,在选项卡控件内有选项卡项.我有WPF用户控件wthin;其中3个具有wpf datagrid.

直到昨天,当我添加了3个中的最后一个时,一切都很好,而且我无法通过单击鼠标在datagrid中选择行.其他2个工作正常.这是有用户控制问题的XAML代码:

I have a WPF app with a tab control, within the tab control there are tab items. I have wpf user controls wthin; 3 of which have wpf datagrids in them.

Everything was fine until yesterday when I added the last of the 3, and I cannot select rows within the datagrid with a mouse click. The other 2 work fine. Here is the XAML code of the one with the problem usercontrol:

<UserControl x:Class="Hadley.SalesPricingCardConsole.UserControls.ExcludeCustomerSections"

             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" 

             mc:Ignorable="d" >
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="25"/>
            <RowDefinition Height="25"/>
            <RowDefinition Height="300"/>
            <RowDefinition Height="25"/>
            <RowDefinition Height="25"/>
            <RowDefinition Height="5"/>
            <RowDefinition Height="25"/>
            <RowDefinition Height="5"/>
            <RowDefinition Height="25*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="125" />
            <ColumnDefinition Width="125" />
            <ColumnDefinition Width="125" />
            <ColumnDefinition Width="125" />
            <ColumnDefinition Width="125" />
            <ColumnDefinition Width="125" />
            <ColumnDefinition Width="125*" />
        </Grid.ColumnDefinitions>
        <Grid.Background>
            <ImageBrush ImageSource="..\Images\silver_fp.jpg" />
        </Grid.Background>
        <Label Content="List of In-Active Customer Sections" Grid.Column="1" Grid.Row="1" Grid.ColumnSpan="3" />
        <DataGrid x:Name="DatagridExcluded" Grid.Column="1" Grid.Row="2" Grid.ColumnSpan="4" AutoGenerateColumns="False" 

                  ItemsSource="{Binding Path=SearchData}" IsReadOnly="False" AlternatingRowremoved="#d4e8c1" >
            <DataGrid.Background>
                <ImageBrush ImageSource="..\Images\silver_fp.jpg" />
            </DataGrid.Background>
            <DataGrid.RowStyle>
                <Style TargetType="{x:Type DataGridRow}" x:Name="DataGridRowStyle" >
                    <EventSetter Event="DataGridRow.MouseDoubleClick" Handler="DatagridExcluded_MouseDoubleClick" />
                </Style>
            </DataGrid.RowStyle>
            <DataGrid.Columns>
                <DataGridTextColumn Header="Customer Name" Width="*" Binding="{Binding Path=CustomerName}" IsReadOnly="False" />
                <DataGridTextColumn Header="Section Number" Width="*" Binding="{Binding Path=SectionNumber}" IsReadOnly="False" />
                <DataGridTextColumn Header="Company Code" Width="*" Binding="{Binding Path=CompanyCode}" IsReadOnly="False" />
            </DataGrid.Columns>
            <DataGrid.ContextMenu>
                <ContextMenu>
                    <MenuItem x:Name="miMakeActive" Header="Make Active" Click="miMakeActive_Click" />
                </ContextMenu>
            </DataGrid.ContextMenu>
        </DataGrid>
        <TextBox x:Name="txtCustomer" Grid.Column="1" Grid.Row="4" Grid.ColumnSpan="2" />
        <TextBox x:Name="txtSection" Grid.Column="3" Grid.Row="4" Grid.ColumnSpan="2" Margin="10,0,0,0" />
        <Button x:Name="btnAddNew"  Grid.Column="3" Grid.Row="6" Grid.ColumnSpan="2" Width="110" 

                HorizontalAlignment="Left" Margin="10,0,0,0" Content="Add New Entry" Click="btnAddNew_Click" />
    </Grid>
</UserControl>



数据进入数据网格,除了我无法选择一行之外,其他一切看起来都很好. ContextMenu可以工作,但似乎是从datagrid标头发送事件,而不是从行发送事件. MouseDoubleClick事件似乎运行良好,但是我的最终用户希望在选择该行时看到一个漂亮的蓝色行.

这是父表单AXML:



The data gets into the datagrid, and everything looks fine except I cannot select a row. The ContextMenu works, but seems to be sending the event from the datagrid header, not the row. The MouseDoubleClick event seems to work fine, but my end users will want to see a nice blue row when selected.

Here is the parent forms AXML:

<Window xmlns:my="clr-namespace:Hadley.SalesPricingCardConsole.UserControls" x:Class="Hadley.SalesPricingCardConsole.SalesPricingCardConsole"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Height="700" Width="920" MinWidth="920" MinHeight="500" Icon="Images\NewIcon.ico" Closing="Window_Closing" Loaded="Window_Loaded">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="900*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="175*"/>
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.Background>
            <ImageBrush ImageSource="Images\wallpaper-Silver.bmp" />
        </Grid.Background>
        <TabControl x:Name="tabMain" Grid.Row="0" SelectionChanged="TabControl_SelectionChanged">
            <TabItem Header="Search Central Database" Visibility="{Binding MainDBAccessVisibility}">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="900*"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="145"/>
                        <RowDefinition Height="100*" />
                    </Grid.RowDefinitions>
                    <Grid.Background>
                        <ImageBrush ImageSource="Images\silver_fp.jpg" />
                    </Grid.Background>
                    <my:Search x:Name="ucSearch" Grid.Row="0" />
                    <my:SearchResultsGrid x:Name="ucSearchResultsGrid" Grid.Row="1" VerticalAlignment="Top" />
                    <!--<Border Grid.Row="1" BorderBrush="DarkGray" BorderThickness="2"/>
                    <Border Grid.Row="0" BorderBrush="DarkGray" BorderThickness="2"/>-->
                </Grid>
            </TabItem>
            <TabItem Header="Group Operations" Visibility="{Binding GroupsVisibility}">
                <DockPanel >
                    <my:Groups x:Name="ucGroups" DockPanel.Dock="Top"/>
                </DockPanel>
            </TabItem>
            <TabItem Header="Configuration" Visibility="Visible">
                <DockPanel >
                    <my:Configuration x:Name="ucConfiguration" DockPanel.Dock="Top"/>
                </DockPanel>
            </TabItem>
            <TabItem Header="Local Saved Sales Pricing Cards" x:Name="tabItemLocalData" Visibility="{Binding LocalDBAccessVisibility}">
                <DockPanel >
                    <my:LocalDataGrid x:Name="ucLocalData" DockPanel.Dock="Top"/>
                </DockPanel>
            </TabItem>
            <TabItem Header="Manage In-Active Customer Sections" x:Name="tabExcludedSections" Visibility="Visible" >
                <DockPanel>
                    <my:ExcludeCustomerSections x:Name="ucExclusions" DockPanel.Dock="Top" />
                </DockPanel>
            </TabItem>
            <!--<TabItem x:Name="tabItemHelp" Header="Help">
                <my:Help />
            </TabItem>-->
        </TabControl>
        <my:MessagePlane x:Name="ucUserMessages" Grid.Row="1" DockPanel.Dock="Bottom" VerticalAlignment="Bottom" />
        <Rectangle x:Name="recBusy" Opacity="0.3" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="1" Grid.RowSpan="3" Visibility="{Binding BusyVisibility}">
            <Rectangle.Fill>
                <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                    <GradientStop Color="LightGray" Offset="0.0" />
                    <GradientStop Color="Gray" Offset="0.25" />
                    <GradientStop Color="SlateGray" Offset="0.75" />
                    <GradientStop Color="WhiteSmoke" Offset="1.0" />
                </LinearGradientBrush>
            </Rectangle.Fill>
        </Rectangle>
    </Grid>
</Window>


The offending User Control is ''ExcludeCustomerSections''.

Here is a working users control''s AXML:


The offending User Control is ''ExcludeCustomerSections''.

Here is a working users control''s AXML:

<UserControl x:Class="Hadley.SalesPricingCardConsole.UserControls.LocalDataGrid"

             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" 

             mc:Ignorable="d" Loaded="UserControl_Loaded" >
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="25"/>
            <RowDefinition Height="25"/>
            <RowDefinition Height="25"/>
            <RowDefinition Height="25"/>
            <RowDefinition Height="500*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="125" />
            <ColumnDefinition Width="125" />
            <ColumnDefinition Width="90" />
            <ColumnDefinition Width="125" />
            <ColumnDefinition Width="125" />
            <ColumnDefinition Width="125" />
            <ColumnDefinition Width="1*" />
        </Grid.ColumnDefinitions>
        <Grid.Background>
            <ImageBrush ImageSource="..\Images\silver_fp.jpg" />
        </Grid.Background>
        <Button x:Name="btnDeleteAllLocalSPCs" Content="Delete ALL local data" Width="150" Height="22" VerticalAlignment="Top" 

                Grid.Row="1" Grid.Column="5" Grid.ColumnSpan="2" HorizontalAlignment="Left" 

                IsEnabled="{Binding GetDataButtonEnabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 

                Click="btnDeleteAllLocalSPCs_Click"/>
        <Button x:Name="btnGetData" Content="Get Local Data" Grid.Row="1" Grid.Column="1" Height="22" VerticalAlignment="Top" IsEnabled="{Binding GetDataButtonEnabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Click="btnGetData_Click"/>
        <Button x:Name="btnSyncData" Width="150" Height="47" Grid.ColumnSpan="2" Grid.Row="1" Grid.RowSpan="2" VerticalAlignment="Top" HorizontalAlignment="Left" Grid.Column="3" 

                IsEnabled="{Binding SyncDataEnabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Click="btnSyncData_Click" >
            <TextBlock>Sync All Local Data with <LineBreak/> Central Database</TextBlock>
        </Button>
        <Button x:Name="btnCreateNewSPC" Content="Create New" Grid.Row="2" Grid.Column="5" Height="22" VerticalAlignment="Top" 

                Click="btnCreateNewSPC_Click" />
        <DataGrid x:Name="dataGridLocalData" Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="7" AutoGenerateColumns="False" 

                  ItemsSource="{Binding Path=SearchData}" MouseDoubleClick="dataGridLocalData_MouseDoubleClick" CellEditEnding="dataGridLocalData_CellEditEnding">
            <DataGrid.Background>
                <ImageBrush ImageSource="..\Images\silver_fp.jpg" />
            </DataGrid.Background>
            <DataGrid.RowStyle>
                <Style TargetType="{x:Type DataGridRow}" x:Name="DataGridRowStyle" >
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=IsEdited}" Value="true">
                            <Setter Property="Background" Value="PaleGreen"/>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Path=IsEdited}" Value="false">
                            <Setter Property="Background" Value="WhiteSmoke"/>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Path=IsNewLocalSPC}" Value="true">
                            <Setter Property="Background" Value="Yellow"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.RowStyle>            
            <DataGrid.Columns>
                <DataGridTextColumn Header="PrimaryKey" Binding="{Binding Path=PrimaryKey}" Visibility="Collapsed" IsReadOnly="True"/>
                <DataGridTextColumn x:Name="colCustomer" Header="Customer" Width="*" Binding="{Binding Path=Customer}" IsReadOnly="True"/>
                <DataGridTextColumn x:Name="colSection" Header="Section Number" Width="*" Binding="{Binding Path=SectionNumber}" IsReadOnly="True"/>
                <DataGridTextColumn x:Name="colDescription" Header="Description" Width="*" Binding="{Binding Path=Description}" IsReadOnly="True" />
                <DataGridTextColumn x:Name="colPPTonne" Header="Price Per Tonne" Width="*" Binding="{Binding Path=PricePerTonne, StringFormat=C, ConverterCulture=en-GB}" IsReadOnly="True"/>
                <DataGridTextColumn x:Name="colPP100M" Header="Price Per 100M" Width="*" Binding="{Binding Path=PricePer100M, StringFormat=C, ConverterCulture=en-GB}" IsReadOnly="True"/>
                <DataGridTextColumn x:Name="colContrib" Header="Contribution Percent" Width="*" Binding="{Binding Path=ContributionPercent, StringFormat=P2}" IsReadOnly="True"/>
                <DataGridTextColumn x:Name="colCreatedDate" Header="Date Created" Width="*" Binding="{Binding Path=DateEntered, StringFormat='{}{0:d-MMM-yyyy}'}" IsReadOnly="True"/>
                <DataGridTextColumn x:Name="colValidFrom" Header="Valid From" Width="*" Binding="{Binding Path=ValidFrom, StringFormat='{}{0:d-MMM-yyyy}'}" IsReadOnly="True"/>
                <DataGridTextColumn x:Name="colCompanyCode" Header="Company Code" Width="*" Binding="{Binding Path=CompanyCode}" IsReadOnly="True"/>
                <DataGridTextColumn Header="Notes" Width="*" Binding="{Binding Path=Notes}" >
                    <DataGridTextColumn.ElementStyle>
                        <Style TargetType="TextBlock">
                            <Setter Property="TextWrapping" Value="Wrap" />
                        </Style>
                    </DataGridTextColumn.ElementStyle>
                </DataGridTextColumn>
                <DataGridTextColumn x:Name="colReferenceNo" Header="Reference No" Width="*" Binding="{Binding Path=ReferenceNumber}" IsReadOnly="True"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</UserControl>


Anybody got any suggestions ?? I''ve tried everything I can think of.

提前谢谢.

Baxter.


Anybody got any suggestions ?? I''ve tried everything I can think of.

Thanks in advance.

Baxter.

推荐答案

It''s fixed the selection changed event was firing for the tab control, even when the selection wasn''t changing(??). So the seletion was being cancelled.

Saw this happening by putting the data access onto another thread. Then I could see the selection/deselction happpening. Before that nothing.

Cheers, BP
It''s fixed the selection changed event was firing for the tab control, even when the selection wasn''t changing(??). So the seletion was being cancelled.

Saw this happening by putting the data access onto another thread. Then I could see the selection/deselction happpening. Before that nothing.

Cheers, BP


这篇关于WPF Datagrid不能通过单击鼠标选择行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆