How to set selecteditem/selected value of a listbox via two way databinding on a xaml page in c#?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I am loading a page to edit an item. The item is an object which has a property called "DecisionItem". That item has a property called "CurrentType", which is type "DecisionType", an enum. On the page is a list box intended to show the possible values of
"CurrentType" so that the user can set them. The list box is meant to show the current value of item selected when the page loads.
Here is the listbox declaration (with prior objects included for context..):
<TextBox x:Name="DecisionTitle" Margin="229,4,0,578" Text="{Binding Title, Mode=TwoWay}"/><br/>
<TextBox x:Name="DecisionSubTitle" Margin="229,66,0,519" Text="{Binding Subtitle, Mode=TwoWay}"/><br/>
<TextBox x:Name="DecisionDescription" Margin="229,125,0,460" Text="{Binding Description,Mode=TwoWay}" /><br/>
<TextBox x:Name="DecisionTitle" Margin="229,4,0,578" Text="{Binding Title, Mode=TwoWay}"/><br/>
<TextBox x:Name="DecisionSubTitle" Margin="229,66,0,519" Text="{Binding Subtitle, Mode=TwoWay}"/><br/>
<TextBox x:Name="DecisionDescription" Margin="229,125,0,460" Text="{Binding Description,Mode=TwoWay}" /><br/>
<ListBox x:Name="DecisionType" Margin="229,185,0,397" SelectedValue="{Binding DecisionItem.CurrentType, Mode=TwoWay} <br/>
</ListBox>
Here is the code that implements DecisionItem.CurrentType:
public DecisionType CurrentType<br/>
{<br/>
get { return m_dtype; }<br/>
set { m_dtype = value; }<br/>
}
Here is the loadstate method in the page in question... note that the itemssource, displaymemberpath and selectedvaluepath are being set here:<br/>
/// <summary><br/>
/// Populates the page with content passed during navigation. Any saved state is also<br/>
/// provided when recreating a page from a prior session.<br/>
/// </summary><br/>
/// <param name="navigationParameter The parameter value passed to<br/>
/// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested.<br/>
/// </param><br/>
/// <param name="pageState A dictionary of state preserved by this page during an earlier<br/>
/// session. This will be null the first time a page is visited.</param><br/>
protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)<br/>
{<br/>
// TODO: Create an appropriate data model for your problem domain to replace the sample data<br/>
var item = SampleDataSource.GetItem((String)navigationParameter);<br/>
this.DefaultViewModel["Item"] = item;
DecisionType.ItemsSource = SampleDataSource.DecisionTypeLabels;<br/>
DecisionType.DisplayMemberPath = "label";<br/>
DecisionType.SelectedValuePath = "dtype";
}
Here is the code that implements SampleDataSource.DecisionTypeLabels;
public static IEnumerable<DecisionTypeLabel> DecisionTypeLabels<br/>
{ get; private set; }
public SampleDataSource()<br/>
{<br/>
DecisionType dt = DecisionType.BestFit;
DecisionTypeLabels = new ObservableCollection<DecisionTypeLabel><br/>
{<br/>
new DecisionTypeLabel{dtype=DecisionType.BestFit,label="Best Fit"},<br/>
new DecisionTypeLabel{dtype=DecisionType.Prioritize,label="Prioritize"},<br/>
new DecisionTypeLabel{dtype=DecisionType.ProCon,label="Pro Or Con"},<br/>
new DecisionTypeLabel{dtype=DecisionType.ThisOrThat,label="This Or That"}
};<br/>
...
When the page is loaded, <br/>
1. All the control "Title", "Subtitle", etc. show the expected value - the binding seems to work<br/>
2. the DecisionType list box shows the values "Best Fit", "Prioritize", "Pro Or Con" and "This or That" as expected.<br/>
3. the DecisionType list box has nothing selected in it - this is unexpected, as the value of "CurrentType" is set going in
If change any value on any control on the page, including in "DecisionType", and hit "Back" on the page<br/>
1. All values - Title, Subtitle... even "CurrentType"... reflect the value previously set<br/>
2. if I load the page again (click "edit"), same result as #3 above when page is loaded - the values in the "DecisionType" list box are unselected
How do I get The DecsionType list box to indicate the correct selected item based on the databinding? I tried explicitly setting the "SelectedIndex" during LoadState on the page, but that gets reset to -1 after the page is loaded.
Thanks!!

View the full article
 
Back
Top