Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are a lot of ways to answer your question. I think the most important of these ways is to emphasize that in WPF, if you're writing code that explicitly manipulates UI controls, you're probably doing something wrong. </p> <p>I can't emphasize this enough. To my mind the <em>primary reason to use WPF at all</em> is that it frees you from having to manipulate the UI in code.</p> <p>Here's how my programs determine whether or not a checkbox is checked:</p> <p>In XAML:</p> <pre><code>&lt;CheckBox IsThreeState="false" IsChecked="{Binding IsChecked, Mode=TwoWay}"/&gt; </code></pre> <p>In the object bound to this view:</p> <pre><code>public bool IsChecked { get; set; } </code></pre> <p>The <code>IsChecked</code> property of my object now always reflects the state of the checkbox in the UI. (The reverse is not true unless I implement change notification in my class.)</p> <p>For your combo box example, I'd implement it like this. First, in XAML:</p> <pre><code>&lt;ComboBox ItemsSource="{Binding Numbers}" SelectedItem="{Binding SelectedNumber, Mode=TwoWay}"/&gt; </code></pre> <p>In the object bound to the view:</p> <pre><code>public IEnumerable&lt;int&gt; Numbers { get { return new[] { 1, 2, 3, 4, 5, 6 }; } } public int? SelectedNumber { get; set; } </code></pre> <p><code>SelectedNumber</code> is nullable in this case so that you can test for the case where nothing was selected, e.g.:</p> <pre><code>Console.WriteLine(SelectedNumber == null ? "No number was selected." : SelectedNumber + " was selected.); </code></pre>
    singulars
    1. This table or related slice is empty.
    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. This table or related slice is empty.
    1. 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