Accessing a MvvM value doesn't work

This forum is meant for questions and discussions about the X# language and tools
Post Reply
ic2
Posts: 1858
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

Accessing a MvvM value doesn't work

Post 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}"
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Accessing a MvvM value doesn't work

Post 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
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
ic2
Posts: 1858
Joined: Sun Feb 28, 2016 11:30 pm
Location: Holland

Accessing a MvvM value doesn't work

Post 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
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Accessing a MvvM value doesn't work

Post 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
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Post Reply