我希望根據按鈕的內容從命令執行某個功能。截至目前,我有兩個按鈕。DesignImg單擊連接時,我需要將其更改為“斷開連接”。如何從我的視圖模型訪問按鈕的內容。
這是我將連接/斷開連接的視圖模型:
namespace firstApp
{
public class ViewModel
{
public TcpClient client = null;
public ICommand MyCommand { get; set; }
public ViewModel()
{
MyCommand = new Command(ExecuteMethod, canExecuteMethod);
}
private bool canExecuteMethod(object parameter)
{
return true;
}
public void ExecuteMethod(object parameter)
{
//if client is not connected do this... id client is connected then close client.
try
{
// Create a TcpClient.
// Note, for this client to work you need to have a TcpServer
// connected to the same address as specified by the server, port
// combination.
Int32 port = 8000;
TcpClient client = new TcpClient("127.0.0.1", port);
}
catch (ArgumentNullException ex)
{
MessageBox.Show(ex.Message.ToString());
}
catch (SocketException ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
這是我發送的視圖模型:
namespace firstApp
{
public class SendViewModel: ViewModel
{
public ICommand MySendCommand { get; set; }
public SendViewModel()
{
MyCommand = new Command(ExecuteMethod, canExecuteMethod);
}
private bool canExecuteMethod(object parameter)
{
return true;
}
public void ExecuteSendMethod(object parameter)
{
if (client.Connected)
{
Byte[] data = System.Text.Encoding.ASCII.GetBytes("hello");
// Get a client stream for reading and writing.
// Stream stream = client.GetStream();
NetworkStream stream = client.GetStream();
// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);
//ChatScreentextBox.AppendText(TextToSend);//probably done need it here
//worker1.RunWorkerAsync();///gets to here
}
else
{
//ChatScreentextBox.AppendText("There is no connection");
}
}
uj5u.com熱心網友回復:
C#
//This variable needs to be usable in xaml fyi
private bool IsConnected = false;
// Your command method
private void ChangeConnectionStatus()
{
if (IsConnected)
{
IsConnected = false;
//call method for connecting bellow
}
else
{
IsConnected = true;
//call method for disconnecting bellow
}
}
XAML
<Button Command="{Binding ChangeConnectionStatus}">
<Button.Style>
<Style TargetType="Button">
<Style.Triggers>
<DataTrigger Binding="{Binding IsConnected}" Value="true">
<Setter Property="Content" Value="Conected" />
</DataTrigger>
<DataTrigger Binding="{Binding IsConnected}" Value="false">
<Setter Property="Content" Value="Disconected" />
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
不是復制和可通過的代碼,但應該給你和如何去做的想法。
同樣在 ViewModel 中,人們通常會盡量避免編輯視圖,這就是為什么 DataTrigger 通常是一種更好的方法,因為它使它們保持分離。
uj5u.com熱心網友回復:
添加 ViewModel 屬性并將其系結到按鈕文本。然后您可以輕松地將按鈕文本更改為其他內容。請注意,您的 ViewModel 需要實作INotifyPropertyChanged并添加適當的事件和事件引發方法。
在 ViewModel 中(假設INotifyPropertyChanged已實作):
private string _connectButtonText = "Connect"; // set initial value here or in the constructor
public string ConnectButtonText { get => _connectButtonText; set => { _connectButtonText = value; RaisePropertyChanged(nameof(ConnectButtonText)); }
在 XAML 中(假設您的頁面DataContext設定為您的 ViewModel):
<Button Text={Binding ConnectButtonText} ... />
要更改按鈕文本,請在您的 ViewModel 中:
public void ExecuteSendMethod(object parameter)
{
...
ConnectButtonText = "Disconnect";
...
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/330498.html
