除非先单击行,否则列表视图行内的WPF MVVM按钮不起作用

时间:2018-06-27 02:59:39

标签: wpf mvvm

enter image description here

我在每行的列表视图中有两个按钮。一个用于显示所选项目,另一个用于显示索引号。用于显示所选项目的按钮可以正常工作,但是用于显示索引号的按钮则无效。

当我单击按钮(显示索引号)时,它返回索引号零“ 0”,表示它无法正常工作。但是,当我选择该行然后单击按钮时,它就起作用了,我的意思是它随后返回了正确的索引号。 我想要的是按钮(显示索引号),无需先选择行即可工作。

下面是xaml代码

<ListView                       
    Grid.Row="1"
    ItemContainerStyle="{StaticResource FileItemStyle}"
    ItemsSource="{Binding CollViewSourceBarCode.View, IsAsync=True}"
    ScrollViewer.HorizontalScrollBarVisibility="Disabled"
    SelectedIndex="{Binding SelectedIndex}"
    SelectedItem="{Binding SelectBarCode, UpdateSourceTrigger=PropertyChanged}"
    SelectionMode="Extended"
    Style="{StaticResource ListItemsMain}"
..

下面是selecteditem的按钮代码,可以正常工作

<Button
   Command="{Binding DataContext.SelectedItemCommand, RelativeSource={RelativeSource AncestorType=ListView}}"
   Content="{Binding SelectedItemContent}" />

c#

private ICommand mSelectedItemCommand;
public ICommand SelectedItemCommand
    {
        get
        {
            if (mSelectedItemCommand== null)
            {
                mSelectedItemCommand= new DelegateCommand(delegate ()
                {
                    MessageBox.Show(SelectBarCode.BarCodeEntry_ID.ToString());

                });
            }
            return mSelectedItemCommand;
        }
    }

及以下代码用于无效的选择索引号

<Button
   Command="{Binding DataContext.SelectedIndexCommand, RelativeSource={RelativeSource AncestorType=ListView}}"
   Content="{Binding SelectedIndexContent}" />

c#

private ICommand mSelectedIndexCommand;
public ICommand SelectedIndexCommand
    {
        get
        {
            if (mSelectedIndexCommand== null)
            {
                mSelectedIndexCommand= new DelegateCommand(delegate ()
                {
                    MessageBox.Show(SelectedIndex.ToString());

                });
            }
            return mSelectedIndexCommand;
        }
    }
下面的

更新是ListViewItem的完整样式

<Style x:Key="FileItemStyle" TargetType="{x:Type ListViewItem}">
        <Setter Property="Margin" Value="5,5,5,5" />
        <Setter Property="Padding" Value="0,0,0,0" />
        <Setter Property="HorizontalAlignment" Value="Left" />
        <Setter Property="Background" Value="Green" />

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListViewItem}">
                    <Grid
                        Width="195"
                        Height="auto"
                        HorizontalAlignment="Left"
                        VerticalAlignment="Top">
                        <Border>
                            <Border.Background>
                                <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
                                    <GradientStop x:Name="GradientStop1" Offset="0.0" Color="#FF2C302C" />
                                    <GradientStop x:Name="GradientStop2" Offset="0.25" Color="#FF3E3C3C" />
                                    <GradientStop x:Name="GradientStop3" Offset="0.75" Color="#FF3E3C3C" />
                                    <GradientStop x:Name="GradientStop4" Offset="1.0" Color="#FF3E3D3C" />
                                </LinearGradientBrush>
                            </Border.Background>

                            <Border.Triggers>
                                <EventTrigger RoutedEvent="Border.MouseEnter">
                                    <BeginStoryboard>
                                        <Storyboard>
                                            <DoubleAnimation
                                                Storyboard.TargetName="GradientStop1"
                                                Storyboard.TargetProperty="Offset"
                                                From="0.0"
                                                To="1.0"
                                                Duration="0:0:0.25" />
                                            <ColorAnimation
                                                Storyboard.TargetName="GradientStop4"
                                                Storyboard.TargetProperty="Color"
                                                To="DarkGray"
                                                Duration="0:0:0.25" />
                                        </Storyboard>
                                    </BeginStoryboard>
                                </EventTrigger>
                                <EventTrigger RoutedEvent="Border.MouseLeave">
                                    <BeginStoryboard>
                                        <Storyboard>
                                            <DoubleAnimation
                                                Storyboard.TargetName="GradientStop1"
                                                Storyboard.TargetProperty="Offset"
                                                From="1.0"
                                                To="0.0"
                                                Duration="0:0:0.25" />
                                            <ColorAnimation
                                                Storyboard.TargetName="GradientStop4"
                                                Storyboard.TargetProperty="Color"
                                                To="#FF3E3C3C"
                                                Duration="0:0:0.25" />
                                        </Storyboard>
                                    </BeginStoryboard>
                                </EventTrigger>



                            </Border.Triggers>
                            <Border.Style>
                                <Style TargetType="Border">
                                    <!--<Setter Property="Background" Value="#FF3E3C3C" />-->
                                    <Setter Property="HorizontalAlignment" Value="Stretch" />
                                    <Setter Property="VerticalAlignment" Value="Stretch" />
                                    <Setter Property="BorderBrush" Value="{x:Null}" />
                                    <Setter Property="BorderThickness" Value="1" />
                                    <Setter Property="CornerRadius" Value="15" />
                                    <Style.Triggers>
                                        <Trigger Property="IsMouseOver" Value="True">
                                            <Setter Property="Cursor" Value="Hand" />

                                        </Trigger>
                                    </Style.Triggers>
                                </Style>
                            </Border.Style>
                        </Border>
                        <StackPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                            <ContentPresenter />
                        </StackPanel>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsKeyboardFocusWithin" Value="True">
                <Setter Property="IsSelected" Value="True" />
            </Trigger>
        </Style.Triggers>

    </Style>

更新2: ObserverableCollection

public ObservableCollection<BarCodeModel> BarCode
    {
        get
        {
            mBarCode = mBarCode ?? new ObservableCollection<BarCodeModel>();
            return mBarCode;
        }
    }

在构造器中很薄,我将其分配给CollViewSourceBarCode

CollViewSourceBarCode = new CollectionViewSource();
CollViewSourceBarCode.Source = BarCode;

所选项目的代码

public BarCodeModel mSelecBarCode;
    public BarCodeModel SelectBarCode
    {
        get => mSelecBarCode;
        set
        {
            if (value != null)
            {
                mSelecBarCode = value;                    
                OnPropertyChanged("SelectBarCode");
            }
        }
    }

selectedindex的代码

private int mSelectedIndex;
    public int SelectedIndex
    {
        get => mSelectedIndex;
        set
        {
            mSelectedIndex = value;
            OnPropertyChanged("SelectedIndex");
        }
    }

1 个答案:

答案 0 :(得分:1)

所以问题出在SelectionMode上。它应该是Single

<ListView                       
    Grid.Row="1"
    ItemContainerStyle="{StaticResource FileItemStyle}"
    ItemsSource="{Binding CollViewSourceBarCode.View, IsAsync=True}"
    ScrollViewer.HorizontalScrollBarVisibility="Disabled"
    SelectedIndex="{Binding SelectedIndex}"
    SelectedItem="{Binding SelectBarCode, UpdateSourceTrigger=PropertyChanged}"
    SelectionMode="Single"
    Style="{StaticResource ListItemsMain}"