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

发布于 2024-10-06 18:05:40 字数 2854 浏览 1 评论 0原文

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

  <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());
 }

ViewModel

     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
                                    };
            }
        }

Cannot figure out why is this not setting a Text property after BidAgent in ViewModel is initial set to some value? Searching and selecting works fine, but initial binding does not. Basically, what I want is when I set the view model (BidAgent) for the view, that it displays the text for the selected item that is created explicitly from the values on the BidAgent. Any ideas how to do this?

  <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>

Code Behind

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

ViewModel

     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
                                    };
            }
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

债姬 2024-10-13 18:05:40

SelectedAgent 属性返回的对象及其在 AvailableAgents 属性中的匹配条目是否有可能实际上是两个不同的对象实例,只是碰巧包含相同的数据?如果是这样,请在返回集合后尝试将 AvailableAgents 中的匹配实例分配给 SelectedAgent

Is it possible that the object returned by the SelectedAgent property and its matching entry in the AvailableAgents property are in fact two distinct object instances that just happen to contain the same data? If so try assigning the matching instance from the AvailableAgents to the SelectedAgent once the set has been returned.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文