Autocomletebox:如何绑定文本属性以显示所选项目是什么?
无法弄清楚为什么在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SelectedAgent
属性返回的对象及其在AvailableAgents
属性中的匹配条目是否有可能实际上是两个不同的对象实例,只是碰巧包含相同的数据?如果是这样,请在返回集合后尝试将AvailableAgents
中的匹配实例分配给SelectedAgent
。Is it possible that the object returned by the
SelectedAgent
property and its matching entry in theAvailableAgents
property are in fact two distinct object instances that just happen to contain the same data? If so try assigning the matching instance from theAvailableAgents
to theSelectedAgent
once the set has been returned.