0

我在表单中使用 Silverlight 的工具包 AutoCompleteBox 控件,希望它显示绑定到 SelectedItem 的值,或者如果它为空,则显示绑定到 Text 属性的值。

问题是当 SelectedItem 为空时,它会自动清除 Text 属性,即使它绑定到具有值的 VM 属性也是如此。

这是一些 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 回答