Autocomletebox: How to bind the text property to show what ever selected item is?
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
};
}
}
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.
精彩评论