2

ビューモデルクラスに、ボタンのクリックによって呼び出され、いくつかの操作を実行するメソッドがあります。これで、xamlファイルにラベルとボタンがあります。

<Label Content="" Height="20" HorizontalAlignment="Center" Margin="0,50,0,0" Name="label1" VerticalAlignment="Top" Width="119" />
<Button Content="Sync" Height="23" Command="{Binding Path=SyncCommand}" HorizontalAlignment="Center" Margin="0,15,0,0" Name="button1" VerticalAlignment="Top" Width="100" />

私のビューモデル:

    // This method is called when Sync Button is Clicked
    public void SyncCommandExecuted()
    {            
        string strBadResp = string.Empty;  
        Byte[] sendBuf = new Byte[256];
        Byte[] readBuf = new Byte[256];          
        sendBuf[0] = 0x80;
        mComm.setAddress(0x3e);
        mComm.WriteBytes(4, sendBuf);

        if (mComm.ReadBytes(4, ref readBuf) != 0)
        {                
            for (int cnt = 0; cnt < 4; cnt++ )
            {
                if (readBuf[cnt] != null)
                {
                    sendBuf[cnt] = readBuf[cnt];                        
                }
                else
                {                        
                    strBadResp = "Bad response";

                    // Here I want to display the content in strBadResp i.e. BAD RESPONSE on a label
                    sendBuf = null;                        
                }
            }

            if (sendBuf != null)
            {
                strBadResp = BitConverter.ToString(sendBuf);

                // Here I want to display the content in strBadResp on a label
            }                
        }
    }

私のReadBytesメソッドは以下を保存します:

byteArray[0] = 0x01;
byteArray[1] = 0x02;
byteArray[2] = 0x03;
byteArray[3] = 0x04;

したがって、基本的に両方の場所で、結果(strBadResp)がラベルに含まれている必要があります。私はそれを明確にしたと思います。私はこのWPFの世界に不慣れです。助けてください!!!

4

2 に答える 2

2

ラベルのコンテンツプロパティをビューモデルのプロパティにバインドします。ラベルを更新する場合は、応答プロパティを更新します。

意見

<Label Content="{Binding Response}" Height="20" HorizontalAlignment="Center" Margin="0,50,0,0" Name="label1" VerticalAlignment="Top" Width="119" />

ViewModel

public class YourViewModel : INotifyPropertyChanged {

    string response;

    public string Response {

        get  { return this.response; }

        set {
            if (this.response == value)
                return;

            this.response = value;
            NotifyPropertyChanged("Response");
        }
    }

    public event NotityPropertyChangedEventHandler  = delegate {}

    void NotifyPropertyChanged(string propertyName) {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName);   
    }
  }
于 2012-10-04T05:56:39.223 に答える
1

以下のように、コードビハインドでLableのオブジェクトを作成する必要があります。

var lableMSG = new Lable();

lableMSG.Content = "Message string";

これがお役に立てば幸いです!!

于 2012-10-04T04:53:18.050 に答える