Laden...

DataGrid SelectedItem SelectedIndex bei Start

Erstellt von Taladan vor 10 Jahren Letzter Beitrag vor 10 Jahren 2.336 Views
Taladan Themenstarter:in
582 Beiträge seit 2008
vor 10 Jahren
DataGrid SelectedItem SelectedIndex bei Start

Ich habe ein Problem mit meinen Datagrid und gebundene Commandos. Und zwar funktioniert der Code. Aber erst, wenn ich das erste mal eine Zelle ausgewählt habe. Wenn das DataGrid noch im Initialisieurngszustand ist, wird immer true zurück geben. Obwohl keine Zeile (sichtbar) markiert wurde.

Es ist wohl so, als würde das DataGrid beim der Initialisierung automatisch die Selection auf den dersten Datensatz legen, aber den nicht als seletierten Eintrag hervorheben.

CmdOperationFirst = new DelegateCommand((param) => OperationFirst(), (param) =>
            {
                bool t = (this.SelectedItem != null && this.SelectedItem.ToString() != "{NewItemPlaceholder}" && this.SelectedIndex != 0);
                return t;
            });
            CmdOperationUp = new DelegateCommand((param) => OperationUp(), (param) =>
            {
                bool t = (this.SelectedItem != null && this.SelectedItem.ToString() != "{NewItemPlaceholder}" && this.SelectedIndex != 0);
                return t;
            });
            CmdOperationDown = new DelegateCommand((param) => OperationDown(), (param) =>
            {
                bool t = (this.SelectedItem != null && this.SelectedItem.ToString() != "{NewItemPlaceholder}" && this.SelectedIndex != -1 && this.SelectedIndex < dg.Items.Count - 2 );
                return t;
            });
            CmdOperationLast = new DelegateCommand((param) => OperationLast(), (param) =>
            {
                bool t = (this.SelectedItem != null && this.SelectedItem.ToString() != "{NewItemPlaceholder}" && this.SelectedIndex != -1 && this.SelectedIndex < dg.Items.Count - 2);
                return t;
            });
            CmdOperationDelete = new DelegateCommand((param) => OperationDelete(), (param) =>
            {
                bool t = (this.SelectedItem != null && this.SelectedItem.ToString() != "{NewItemPlaceholder}");
                System.Diagnostics.Debug.Write(DateTime.Now);
                System.Diagnostics.Debug.Write(" ");
                System.Diagnostics.Debug.WriteLine(t);
                return t;
            });
 private object _SelectedItem = null;
        /// <summary>
        /// Gibt das selektierte Objekt aus dem Datagrid an.
        /// </summary>
        public object SelectedItem
        {
            get { return _SelectedItem; }
            set { _SelectedItem = value; NotifyPropertyChanged(); }
        }

        private int _SelectedIndex = -1;
        /// <summary>
        /// Gibt den Index des Datagrids an.
        /// </summary>
        public int SelectedIndex
        {
            get { return _SelectedIndex; }
            set { _SelectedIndex = value; NotifyPropertyChanged(); }
        }

        private object _CurrentItem = null;
        /// <summary>
        /// Gibt das aktuell ausgewählte Item im Grid an.
        /// </summary>
        public object CurrentItem
        {
            get { return _CurrentItem; }
            set { _CurrentItem = value; NotifyPropertyChanged(); }
        }
        
<DataGrid CanUserReorderColumns="True" 
                      CanUserResizeColumns="True" 
                      CanUserResizeRows="True" 
                      CanUserSortColumns="False"
                      AutoGenerateColumns="False" 
                      CanUserDeleteRows="True"
                      HeadersVisibility="Column"
                      IsSynchronizedWithCurrentItem="True"
                      x:Name="dg"
                      SelectionMode="Single"
                      CurrentItem="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:UcWenn}}, Path=CurrentItem, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, Mode=TwoWay}"
                      SelectedItem="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:UcWenn}}, Path=SelectedItem, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, Mode=TwoWay}"
                      SelectedIndex="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:UcWenn}}, Path=SelectedIndex, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, Mode=TwoWay}"
                      AlternatingRowBackground="AntiqueWhite"  
                      AlternationCount="2"
                      ItemsSource="{Binding Operations, Mode=TwoWay, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}"
                  >

Gruß dat Tala

5.299 Beiträge seit 2008
vor 10 Jahren

tja, wenn true zurückgegeben wird, dann muß wohl SelectedItem != null sein - das kann man ja mit einem Haltepunkt überprüfen.

Der frühe Apfel fängt den Wurm.