• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

Autocomletebox:如何绑定文本属性以显示曾经选择的项目是什么?

silverlight 来源:epitka 3次浏览

无法弄清楚为什么ViewModel中的BidAgent初始设置为某个值后,这不会设置Text属性?搜索和选择工作正常,但初始绑定没有。基本上,我想要的是当我为视图设置视图模型(BidAgent)时,它显示了从BidAgent上的值显式创建的选定项目的文本。任何想法如何做到这一点?Autocomletebox:如何绑定文本属性以显示曾经选择的项目是什么?

<i:Interaction.Triggers> 
     <i:EventTrigger EventName="AgentSearchCompleted" SourceObject="{Binding}"> 
      <ei:CallMethodAction TargetObject="{Binding ElementName=ctlAgentSearchBox}" MethodName="PopulateComplete" /> 
     </i:EventTrigger> 
    </i:Interaction.Triggers> 
<sdk:AutoCompleteBox Name="ctlAgentSearchBox" Width="300" Margin="0,5,0,0" HorizontalAlignment="Left" ItemsSource="{Binding AvailableAgents}" 
SelectedItem="{Binding SelectedAgent}" FilterMode="None" ValueMemberPath="SearchDisplayString" MinimumPrefixLength="1"> 
<sdk:AutoCompleteBox.ItemTemplate> 
     <DataTemplate> 
      <TextBlock Text="{Binding SearchDisplayString}"/> 
     </DataTemplate> 
</sdk:AutoCompleteBox.ItemTemplate> 
</sdk:AutoCompleteBox> 

代码隐藏

public void GetActiveAgentsByNumber(object sender, PopulatingEventArgs e) 
{ 
    e.Cancel = true; 
    (DataContext as BidAgentEditViewModel).GetActiveAgentsByNumber(number.ToString()); 
} 

视图模型

 public void GetActiveAgentsByNumber(string agentNumber) 
    { 
     _bidAgentDataService.GetActiveAgentsByNumber(agentNumber, getActiveAgentsByNumberCallback); 
     } 
     private void getActiveAgentsByNumberCallback(IEnumerable<AgentSearchDto> result) 
     { 
      AvailableAgents = result; 
      Event.Raise(AgentSearchCompleted, this); 
     } 

    private AgentSearchDto _selectedAgent; 
     public AgentSearchDto SelectedAgent 
     { 
      get { return _selectedAgent; } 

      set 
      { 
       _selectedAgent = value; 

       BidAgent.AgentId = Int32.Parse(_selectedAgent.Id); 
       BidAgent.AgentName = _selectedAgent.FullName; 
       BidAgent.AgentNumber = _selectedAgent.Number; 
       BidAgent.AgencyName = _selectedAgent.AgencyName; 

       RaisePropertyChanged(()=>SelectedAgent); 
      } 
     } 

    private BidAgentDto _bidAgent; 
     public BidAgentDto BidAgent 
     { 
      get { return _bidAgent; } 
      private set 
      { 
       _bidAgent = value; 
       RaisePropertyChanged(() => BidAgent); 
       SelectedAgent = new AgentSearchDto() 
            { 
             Id = _bidAgent.AgentId.ToString(), 
             Number = _bidAgent.AgentNumber, 
             FullName = _bidAgent.AgentName 
            }; 
      } 
     } 

===========解决方案如下:

是否有可能由SelectedAgent财产,并在AvailableAgents财产与其匹配的条目返回的对象是其实两个特点t碰巧包含相同数据的对象实例?如果是这样,请尝试将匹配的实例从AvailableAgents分配到SelectedAgent,一旦返回该集合。


版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)