Element auswählen



  • Hallo

    In einer Listview habe ich ein Element auf IsEnabled = false gesetzt. Das Element lässt sich nun nicht mehr über die Maus auswählen. Leider aber noch über Pfeiltasten.

    Wie bekomme ich das hin dass man das Element gar nicht mehr auswählen kann.



  • Moin,

    ohne das jetzt selbst nachzubauen bla bla bla... Hast du es schon mal mit IsHitTest Dingsbums oder IsTabStop bla ausprobiert? beides würde ich mal auf false setzen und dann nochmal ausprobieren.



  • Das ganze ist etwas anders. Ich erklärs nochmal genau. Habe bei der Beschreibung zu schnelle geschossen.

    Es geht um folgendes:

    Ich habe eine Listbox (oder Listbox Problem ist das selbe) in der ich mehrere Einträge anzeige. Nun möchte ich sagen wir mal das erste Element unsichtbar machen.

    Wenn ich das ganze nun über den Content mache und einfach ein paar ListBoxItems einfüge und dass hier beim ersten Element die Property Visibility auf Colapsed oder auch Hiden setze, ist dieses nicht mehr sichtbar und ich kann es auch nicht auswählen.

    Wenn ich die Elemente aber in einer ObservableCollection halte und die an die ItemsSource binde und ist das Item zwar nicht mehr sichtbar ich kann es aber trotzdem auswählen. Und hier helfen weden IsEnabled, IsHitTestVisible noch sonst was.

    Ich poste mal eine kleines Beipiel



  • <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Name="TheWindow"
            Title="MainWindow"
            Width="525"
            Height="350">
        <Grid>
            <ListBox Name="listBox1"
                     Width="221"
                     Height="141"
                     HorizontalAlignment="Left"
                     VerticalAlignment="Top"
                     IsSynchronizedWithCurrentItem="True"
                     SelectedItem="{Binding ElementName=TheWindow,
                                            Path=ActiveAnimal}">
                <ListBoxItem Content="Elefant" Visibility="Hidden" />
                <ListBoxItem Content="Hund" />
                <ListBoxItem Content="Katze" />
                <ListBoxItem Content="Maus" />
                <ListBoxItem Content="Salamander" />
            </ListBox>
            <TextBox Name="textBox1"
                     Width="176"
                     Height="33"
                     HorizontalAlignment="Right"
                     VerticalAlignment="Top"
                     Text="{Binding Path=ActiveAnimal.Content,
                                    ElementName=TheWindow}" />
    
            <ListBox Name="listBox2"
                     Width="221"
                     Height="144"
                     HorizontalAlignment="Left"
                     VerticalAlignment="Bottom"
                     IsSynchronizedWithCurrentItem="True"
                     ItemsSource="{Binding ElementName=TheWindow,
                                           Path=Humans}"
                     SelectedItem="{Binding ElementName=TheWindow,
                                            Path=ActiveHuman}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock 
                                   Text="{Binding Name}"
                                   Visibility="{Binding Vis}" 
                                 />
                        <!-- Alles hat keine Auswirkung-->
                        <!-- IsEnabled="{Binding IsEnabled}" -->
                        <!-- IsHitTestVisible="{Binding IsEnabled}" -->
                        <!-- auch das nicht-->
                        <!-- IsEnabled="False" -->
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
            <TextBox Name="textBox2"
                     Width="176"
                     Height="33"
                     HorizontalAlignment="Right"
                     VerticalAlignment="Bottom"
                     Text="{Binding Path=ActiveHuman.Name,
                                    ElementName=TheWindow}" />
        </Grid>
    </Window>
    
    using System.Collections.ObjectModel;
    using System.Windows;
    using System.Windows.Controls;
    
    namespace WpfApplication1
    {
        /// <summary>
        /// Interaktionslogik für MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
    
                Humans = new ObservableCollection<Human>
                             {
                                 new Human("Paul", Visibility.Hidden,false),
                                 new Human("Markus"),
                                 new Human("Sussanne"),
                                 new Human("Yvonne"),
                                 new Human("Sonja")
                             };
    
                InitializeComponent();
    
            }
    
            public ListBoxItem ActiveAnimal
            {
                get { return (ListBoxItem)GetValue(ActiveAnimalProperty); }
                set { SetValue(ActiveAnimalProperty, value); }
            }
    
            // Using a DependencyProperty as the backing store for ActiveAnimal.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty ActiveAnimalProperty =
                DependencyProperty.Register("ActiveAnimal", typeof(ListBoxItem), typeof(MainWindow), new UIPropertyMetadata(null));
    
            public Human ActiveHuman
            {
                get { return (Human)GetValue(ActiveHumanProperty); }
                set { SetValue(ActiveHumanProperty, value); }
            }
    
            // Using a DependencyProperty as the backing store for ActiveHuman.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty ActiveHumanProperty =
                DependencyProperty.Register("ActiveHuman", typeof(Human), typeof(MainWindow), new UIPropertyMetadata(null));
    
            public ObservableCollection<Human> Humans
            {
                get { return (ObservableCollection<Human>)GetValue(HumansProperty); }
                set { SetValue(HumansProperty, value); }
            }
    
            // Using a DependencyProperty as the backing store for Humans.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty HumansProperty =
                DependencyProperty.Register("Humans", typeof(ObservableCollection<Human>), typeof(MainWindow), new UIPropertyMetadata(null));
        }
    
        public class Human
        {
            public Human(string name, Visibility vis = Visibility.Visible, bool isEnabled=true)
            {
                Name = name;
                Vis = vis;
                IsEnabled = isEnabled;
            }
    
            public string Name { get; set; }
            public Visibility Vis { get; set; }
            public bool IsEnabled { get; set; }
        }
    }
    



Anmelden zum Antworten