none
Composite WPF ICommand RRS feed

  • Question

  • Hi. I want to create a module that would be initiated and received messages from my simple test service. But i have a problem with ICommanding:
    In Property Text  of ViewModel don't work method 'set', but 'get' works fine! How do I fix this?

    public class UIViewModel : INotifyPropertyChanged
        {
            private IService service;
            private ICommand changeTextCommand;
            private IEventAggregator eventAggregator;
            public event PropertyChangedEventHandler PropertyChanged;

            public ICommand ChangeTextCommand
            {
                get
                {
                    if (changeTextCommand == null)
                        changeTextCommand = new DelegateCommand<string>(OnChangeCommandExecuted);
                    return changeTextCommand;
                }
            }

            public void OnChangeCommandExecuted(string text)
            {
                service.SetText(text);
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Text"));
                }
                eventAggregator.GetEvent<TextChangedEvent>().Publish(service.GetText());
            }

            public string Text
            {
                get { return service.GetText(); }
                set { service.SetText(value); }
            }
            public UIViewModel(IService service, IEventAggregator eventAggregator)
            {
                this.service = service;
                this.eventAggregator = eventAggregator;
                eventAggregator.GetEvent<TextChangedEvent>().Subscribe(onTextChangedReceived);
            }
          
            public void onTextChangedReceived(string text)
            {
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("Text"));
            }
        }

        public class TextChangedEvent : CompositePresentationEvent<string> {    }
    Sunday, January 24, 2010 2:14 PM

All replies

  • Use this : 

    public string Text { get { return service.GetText(); } set { service.SetText(value); PropertyChanged(this, new PropertyChangedEventArgs("Text")); } }

    Sunday, January 24, 2010 8:09 PM
  • I changed the code but problem the same. In the button click event breakpoint does not stop neither the
    '  ICommand ChangeTextCommand { get }  '    nor the   '  public void OnChangeCommandExecuted(string text) {}  '  .

    UIView.xaml:
    xmlns:cmd="clr-namespace:Microsoft.Practices.Composite.Presentation.Commands;assembly=Microsoft.Practices.Composite.Presentation"
        <StackPanel x:Name="LayoutRoot" Orientation="Vertical">
            <Label Content="{Binding Path=Text}" Height="28" Name="label1" Width="120" Visibility="Visible" />
            <TextBox Height="23" Name="textBox1" Width="120" />
            <Button Content="Button" Height="23" Name="button1" Width="115" cmd:Click.Command="{Binding ChangeTextCommand, ElementName=LayoutRoot}" cmd:Click.CommandParameter="{Binding Text, ElementName=textBox1}"/>
        </StackPanel>
    Monday, January 25, 2010 4:24 AM
  • I found various options for ' Click.Command Binding' property declaration : DataContext.Command_name, Commands.Command_name...
    Works with:
    <Button Content="Button" Height="23" Name="button1" Width="115"
    cmd:Click.Command="{Binding Path=DataContext.ChangeTaskCommand, ElementName=LayoutRoot}"
    cmd:Click.CommandParameter="{Binding Path=Text, ElementName=textBox1}"/>
    Thanks for answer, but please explain -  why twice declare
            public string Text
            {
                get { return service.Get
    Text (); }
                set
                {
                    service.Set
    Text (value);
                    PropertyChanged(this, new PropertyChangedEventArgs("Text"));  //
    here
                }
            }
    and
            public void OnChangeCommandExecuted(string
    text )
            {
                service.Set
    Text ( text );
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Text")); // and here
                }
                eventAggregator.GetEvent<
    Text ChangedEvent>().Publish(service.Get Text ());
            }


    Where should I call my wcf service better?
    Monday, January 25, 2010 6:35 AM