Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can accomplish this by using a <code>Property</code> of your class such as <code>IsSelected</code>. Here is some sample code for what that <code>Property</code> would look like.</p> <pre><code>public bool IsSelected { get { return isSelected; //return the value of isSelected } set { if (isSelectable) //if the item is allowed to be selected { //then update the value of isSelected isSelected = value; PropertyChanged(this, new PropertyChangedEventArgs("IsSelected")); } } } </code></pre> <p>In this example it is assumed that <b>"isSelectable"</b> is a value that is set in the constructor when your class is being initialized. If you don't already have <code>INotifyPropertyChanged</code> implemented, it needs to be... and the <code>PropertyChanged</code> event needs to be declared.</p> <p>In addition, you will also want a <code>Style</code> that binds the selection of <code>ListView</code> items to this property. Here is an example of a <code>Style</code> that could accomplish this.</p> <pre><code>&lt;ListView.Resources&gt; &lt;Style TargetType="{x:Type ListViewItem}"&gt; &lt;Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/&gt; &lt;/Style&gt; &lt;/ListView.Resources&gt; </code></pre> <p>You can take this one step further, and also bind to the <code>Focusable</code> <code>Property</code>. This will make it so that when you press <kbd>SHIFT</kbd>+<kbd>DOWN</kbd> it will <i>skip</i> any items that you cannot select and select the next item that is selectable. I would accomplish this in a manner similar to the following.</p> <pre><code>public bool Focusable { get { return isSelectable; //if the item is selectable, then it is focusable } } </code></pre> <p>You would also need to create a binding for this in your <code>Style</code> by creating an additional <code>Setter</code>. This could be done the following way, inside the same <code>Style</code> as before.</p> <pre><code>&lt;Setter Property="Focusable" Value="{Binding Focusable}"/&gt; </code></pre> <p>Try it both with and without that last <code>Setter</code> and see which implementation suits your needs best.</p> <p>--------- UPDATE ---------</p> <p>If you would like to only select items on mouse click, you could do so by subscribing to the <code>ListView</code> <code>PreviewMouseLeftButtonDown</code> event. Inside of the event handler, you would need to first <a href="http://blog.nostatic.org/2007/12/wpf-listview-getting-clicked-item.html" rel="nofollow">get the clicked item</a>, and call a function on your class which will override the selection. Here is an example of how that is done.</p> <p>This function would exist in your UI code-behind and must be subscribed to by your <code>ListView</code>:</p> <pre><code>private void myListView_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { DependencyObject dep = (DependencyObject)e.OriginalSource; while ((dep != null) &amp;&amp; !(dep is ListViewItem)) { dep = VisualTreeHelper.GetParent(dep); } if (dep == null) return; object obj = myListView.ItemContainerGenerator.ItemFromContainer(dep); if (obj is MyClass) { foreach (MyClass i in myListView.Items) i.OverrideSelect(false); //unselect all items MyClass item = (MyClass)obj; item.OverrideSelect(true); //select the item clicked } } </code></pre> <p>(Note: replace "MyClass" with your class type).</p> <p>This function would exist in your class file:</p> <pre><code>public void OverrideSelect(bool selected) { isSelected = selected; PropertyChanged(this, new PropertyChangedEventArgs("IsSelected")); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload