Accessing a MvvM value doesn't work
Posted: Fri Jul 12, 2019 4:35 pm
In a mixed X#/C# program I have a user control showing messages in a chat like series of balloons defined in WPF as a user control. This is C# code. The client wants to be able to copy the content of a selected message to the clipboard. Now I am as far as getting in a method on Copy:
private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
// Called from WPF USer control using <CommandBinding Command="Copy" Executed="CommandBinding_Executed"/>
{
string cValue;
cValue = MessagesItemsCtrl.SelectedValue.Message; // Should be message in selected control
// Put cValue in clipboard
But I am unable to retrieve the message content. MessagesItemsCtrl.SelectedValue is the right element on a ListView and I can see all fields in the class below (I show the first view lines) in the Immediate Window.
But cValue = MessagesItemsCtrl.SelectedValue.Message doesn't work. I can not access Message:
Error CS1061 'object' does not contain a definition for 'Message' and no accessible extension method 'Message' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
All attempts to access ChatMessage (ChatMessage.Message or something) instead didn't work either
What do I miss?
Below parts of the code:
Dick
private ChatMessage _selectedChatMessage;
public ChatMessage SelectedChatMessage
{
get { return _selectedChatMessage; }
set
{
_selectedChatMessage = value;
OnPropertyChanged();
}
}
public class ChatMessage : ViewModelBase
{
public int Recno { get; set; }
public string Message { get; set; }
(etc)
XAML : ListView witin the Custom Control, part of code
<ListView x:Name="MessagesItemsCtrl" Grid.Column="1" Margin="0,5,0,0"
ItemsSource="{Binding SelectedParticipant.Chatter}"
ItemTemplate="{DynamicResource MessagesDataTemplate}"
SelectedItem="{Binding SelectedChatMessage, UpdateSourceTrigger=PropertyChanged}"
private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
// Called from WPF USer control using <CommandBinding Command="Copy" Executed="CommandBinding_Executed"/>
{
string cValue;
cValue = MessagesItemsCtrl.SelectedValue.Message; // Should be message in selected control
// Put cValue in clipboard
But I am unable to retrieve the message content. MessagesItemsCtrl.SelectedValue is the right element on a ListView and I can see all fields in the class below (I show the first view lines) in the Immediate Window.
But cValue = MessagesItemsCtrl.SelectedValue.Message doesn't work. I can not access Message:
Error CS1061 'object' does not contain a definition for 'Message' and no accessible extension method 'Message' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
All attempts to access ChatMessage (ChatMessage.Message or something) instead didn't work either
What do I miss?
Below parts of the code:
Dick
private ChatMessage _selectedChatMessage;
public ChatMessage SelectedChatMessage
{
get { return _selectedChatMessage; }
set
{
_selectedChatMessage = value;
OnPropertyChanged();
}
}
public class ChatMessage : ViewModelBase
{
public int Recno { get; set; }
public string Message { get; set; }
(etc)
XAML : ListView witin the Custom Control, part of code
<ListView x:Name="MessagesItemsCtrl" Grid.Column="1" Margin="0,5,0,0"
ItemsSource="{Binding SelectedParticipant.Chatter}"
ItemTemplate="{DynamicResource MessagesDataTemplate}"
SelectedItem="{Binding SelectedChatMessage, UpdateSourceTrigger=PropertyChanged}"