CurrentPosition财产没有被正确地更新 [英] CurrentPosition property not being updated properly

查看:101
本文介绍了CurrentPosition财产没有被正确地更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我再试一次...这里是XAML。你可以看到CollectionViewSource,使用它作为一个DataContext,ListView控件和删除按钮的网格。正在发生的事情是,当我在ListView在一行点击(和样式触发器触发选择ListViewItem的)被选中的行。当我点击删除按钮的onclick火灾,但CurrentPosition属性设置为-1。什么是preventing被更新CurrentPosition属性。

Let me try again...here is the XAML. You can see the CollectionViewSource, the Grid that uses it as a DataContext, the ListView, and the Delete button. What is happening is that when i click on a row in the ListView (and the Style trigger fires to select the ListViewItem) the row is selected. When i click the Delete button the onclick fires but the CurrentPosition property is set to -1. What is preventing the CurrentPosition property from being updated.

XAML

<Window x:Class="PretzelsUI.Admin.Ovens"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="" 
    Height="344" 
    Width="474" 
    mc:Ignorable="d" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:my="clr-namespace:Pretzels.Model;assembly=Pretzels.Model"
    ResizeMode="NoResize"
    WindowStartupLocation="CenterOwner"
    Background="{StaticResource WindowGradient}"       
    Loaded="Window_Loaded">
<Window.Resources>
    <CollectionViewSource x:Key="ovenViewSource" d:DesignSource="{d:DesignInstance my:Oven, CreateList=True}" />
</Window.Resources>
<Grid DataContext="{StaticResource ovenViewSource}">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Border Grid.Row="0" BorderBrush="{StaticResource formTitleBorderBrush}" BorderThickness="2" Name="border1" CornerRadius="30" Padding="7" Background="{StaticResource formTitleBackgroundBrush}" VerticalAlignment="Center" Margin="11">
        <TextBlock Name="textBlock1" Text="Ovens" FontSize="18" FontWeight="Bold" Foreground="{StaticResource formTitleForegroundBrush}" />
    </Border>
    <ListView IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding}" Name="ovenListView" SelectionMode="Single" Height="177"  Grid.Row="1" TabIndex="2" Margin="5,5,5,0">
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="Control.HorizontalContentAlignment" Value="Stretch" />
                <Setter Property="Control.VerticalContentAlignment" Value="Stretch" />
                <!--<Style.Triggers>
                    <Trigger Property="IsKeyboardFocusWithin" Value="True">
                        <Setter Property="IsSelected" Value="True" />
                    </Trigger>
                </Style.Triggers>-->
            </Style>
        </ListView.ItemContainerStyle>
        <ListView.View>
            <GridView>                    
                <GridViewColumn x:Name="nameColumn" Header="Name" Width="100">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Margin="-6,-1" Text="{Binding Path=OvenName, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true, UpdateSourceTrigger=PropertyChanged}" Width="Auto" />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn x:Name="descriptionColumn" Header="Description" Width="300">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Margin="-6,-1" Text="{Binding Path=OvenDescription, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" Width="Auto" MaxLength="100"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
    <StackPanel Grid.Row="2" Name="stackPanel2" Margin="0,30,0,0" Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center">
        <Button Content="New" Height="23"  Margin="2,0,2,0" TabIndex="3" Name="btnAdd" Width="75" Click="btnInsrt_Click" />
        <Button Content="Save" Height="23" Margin="2,0,2,0" TabIndex="4" Name="btnSave" Width="75" Click="btnSave_Click" />
        <Button Content="Delete" Height="23" Margin="2,0,2,0" TabIndex="5" Name="btndelete" Width="75" Click="btndelete_Click" />
    </StackPanel>
</Grid>

C#

 public partial class Ovens : Window
{
    private PretzelEntities dbcontext = new PretzelEntities();
    //private OvenCollection EntityData;
    private CollectionViewSource ViewSource;
    private BindingListCollectionView OvenView;

    public Ovens()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        ViewSource = (CollectionViewSource)this.FindResource("ovenViewSource");

        ViewSource.Source = from s in dbcontext.Ovens select s;
        OvenView = (BindingListCollectionView)(ViewSource.View);
    }


    private void btndelete_Click(object sender, RoutedEventArgs e)
    {
            if (OvenView.CurrentPosition > -1)
            {
                if (MessageBox.Show("Do you really want to delete this Oven?", "Delete Confirmation", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                {
                    this.OvenView.RemoveAt(this.OvenView.CurrentPosition);
                }
            }
            else
            {
                MessageBox.Show("Nothing to Delete.", "Error", MessageBoxButton.OK);
            }
    }




enter code here


我认为正在发生的事情是,当触发器在的ListCollectionView的CURRENTITEM / CurrentPosition没有被正确地更新。我不知道手动去这样做(虽然我知道可用的方法)当我在一行中的一个文本框点击。不知道该怎么做,我可能只是位于手动使用VisualTreeHelper所选的ListViewItem。


I think what is happening is that when the trigger fires the CurrentItem/CurrentPosition of the ListCollectionView is not being properly updated. I am not sure to go about doing this (although I know the methods available) manually when i click on a textbox in one of the rows. Not sure what to do so i may just manually located the selected ListViewItem using the VisualTreeHelper.

推荐答案

我终于放弃了对款式触发,只是处理的GotFocus事件为ListView行内的文本框/组合框。在一侧的处理,我是能够得到的ListViewItem,并设置IsSelected为true。工程就像一个冠军。

I finally gave up on the style trigger and just handled the GotFocus event for the textbox/combobox inside the ListView rows. In side the handler i was able to get the ListViewItem and set the IsSelected to true. Works like a champ.

private void TextBox_GotFocus(object sender, RoutedEventArgs e)
    {
        Control ctrl = sender as Control;
        ContentPresenter cp = (ContentPresenter)ctrl.TemplatedParent;
        GridViewRowPresenter gp = (GridViewRowPresenter)cp.Parent;
        Grid g = (Grid)gp.Parent;
        ListViewItem lvi = (ListViewItem)g.TemplatedParent;
        lvi.IsSelected = true;
    }

这篇关于CurrentPosition财产没有被正确地更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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