0

I have a working HTML form with a listbox from where the user can select from a list of standard bicycle tyre sizes. Each size maps internally to some numerical value, making the data source a Dictionary (map of key/value pairs).

The javascript implementation is as follows, and I would like to have the same behaviour in WPF with databinding (defining the dictionary/enum inside my domain-model class):

<select name="tiresize" id="tiresize"
    onchange="calcula();" style="width: 100px">
    <option value="305">26 X 1.0 (559 mm)</option>
    <option value="311">26 x 1 (650C)</option>
    <option value="312">26 X 1.5</option>
    <option value="324">26 X 1.9</option>
    <option value="330">26 X 1 3/8 (590 mm)</option>
    <option value="340">27 X 1</option>
    <option value="342">27 X 1 1/8</option>
    <option value="343">27 X 1 1/4</option>
    <option value="345">27 X 1 3/8</option>
    <option value="330">26 X 2.125</option>
    <option value="332">700 x 21</option>
    <option value="334" selected="selected">700 x 23</option>
    <option value="335">700 x 25</option>
    <option value="336">700 x 28</option>
    <option value="342">700 x 32</option>
    <option value="345">700 x 35</option>
    <option value="347">700 x 38</option>
    <option value="354">700 x 44</option>
    <option value="365">700 x 50</option>
    <option value="370">700 x 56</option>
</select>

Thanks for any help!

4

1 回答 1

1

You first need to set the DataContext of something (Window, Grid, etc.) that contains your ListBox to an instance of the class that contains the Dictionary. You can do this in XAML or in code. Then:

<ListBox ItemsSource="{Binding MyDictionaryProperty}" DisplayMemberPath="Key"
         SelectedValuePath="Value" SelectedValue="{Binding wheelRadius}"/>

As long as wheelRadius is of type int and has a public setter this will take the Value property of the SelectedItem (thanks to SelectedValuePath) and push that into SelectedValue, which will set your wheelRadius property through a TwoWay Binding (the default for SelectedValue, SelectedItem, and some others).

于 2013-01-30T18:38:46.083 回答