0

SelectedItem にバインドされた値を表示するフォームで、Silverlight のツールキット AutoCompleteBox コントロールを使用しています。それが null の場合は、Text プロパティにバインドされた値を表示します。

問題は、SelectedItem が null の場合、値を持つ VM プロパティにバインドされている場合でも、Text プロパティが自動的にクリアされることです。

ここにいくつかの XAML があります:

<c:AutoCompleteBox
            MinimumPopulateDelay="500"
            ItemsSource="{Binding SuburbSearchResults}"
            SelectedItem="{Binding SelectedSuburb}"
            Text="{Binding SuburbText, Mode=OneWay}"
            MinimumPrefixLength="3" />
4

1 に答える 1

0

First of all using Binding on Text and SelectedItem properties together ? Maybe it's not a good idea.

Its right when you use binding on SelectedItem this manages Text properties value for you.

If you use a ViewModel, I suggest you to bind one property of AutocompleteBox and just use SuburbText prop. in VM. (or just bind SelectedItem and you may use ValueMemberPath with it)

Edit 1:

//Suppose myVM.SuburbText is a local variable in VM, this shows Text prp. binding
//But I prefer Object binding with ValueMemberPath,you may use one of them
//But not both together

public string TextWillBeBound
{
  get
  {
    if(SearchResults.SelectedItem!=null)
    {
      myVM.SuburbText=SearchResults.SelectedItem.TextProperty;
    }
  else if(myVM.SuburbText="")
 {
   myVM.SuburbText="Please write...";
 }
  return myVM.SuburbText;
}
set
 {
   if(SearchResults.SelectedItem==null)     
  { 
    myVm.SuburbText=value; 
    //with value you may create Suburb object ? and set as Selected. 
    //Depending  on what you aim. I suggest using SelectedItem & ValueMemberPath
  }
 }
}

//How did I used this control before

//You can also bind SelectedItem and use ValueMemberPath as shown below.

<sdk:AutoCompleteBox MinimumPopulateDelay="500" MinimumPrefixLength="3"
                     Populating="AutoCompleteBox_Populating"
                     SelectedItem="{Binding Path=SELECTEDITEM,Mode=TwoWay}"
                     ValueMemberPath="DESCRIPTION">
于 2012-10-09T07:11:56.363 に答える