xsharp.eu • Accessing a MvvM value doesn't work
Page 1 of 1

Accessing a MvvM value doesn't work

Posted: Fri Jul 12, 2019 4:35 pm
by ic2
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}"

Accessing a MvvM value doesn't work

Posted: Fri Jul 12, 2019 5:48 pm
by wriedmann
Hi Dick,

what is the type of the SelectedParticipant.Chatter object? An ObservableCollection of ChatMessage objects?

I have some of these constructs working, but in plain X# without any XAML or C#.

Wolfgang

Accessing a MvvM value doesn't work

Posted: Fri Jul 12, 2019 7:26 pm
by ic2
I think SelectedParticipant is a field in he ViewModel representing the user of the chat. But the problem is the message.

I do have a workaround to access the value. But I am sure this is not how I should do it in MvvM....

1 In the set of public ChatMessage SelectedChatMessage I add:
ExtensionMethods.SetLastUsedMessage(_selectedChatMessage.Message);

2 In the class ExtensionMethods I then assign the Message as passed above to a public variable of that class
3 In the CommandBinding_Executed method, called when someone uses Ctrl C I retrieve that value again:
cValue= ExtensionMethods.GetLastUsedMessage("");

It's like defining a program GLOBAL in VO and use that to get an otherwise inaccessible variable the easy way....

Dick

Accessing a MvvM value doesn't work

Posted: Sat Jul 13, 2019 3:54 am
by wriedmann
Hi Dick,
in MVVM you should work with databinding and possibly no fixed connection between view and vievmodel.
You can create your own control class, create a new property and use databinding to bind it to the relativer property of your viewmodel.
Wolfgang