I have a WPF
application using mah metro
where I have a tab control
<Controls:MetroAnimatedTabControl Grid.Column="3" Grid.Row="1" SelectedIndex="{Binding SelectedTabIndex,Mode=TwoWay}">
<TabItem Header="Manual Control">
<view:ManualControlView DataContext="{Binding ManualControlViewModel}"/>
</TabItem>
<TabItem Header="Set up">
<view:SetupView DataContext="{Binding SetupViewModel}"/>
</TabItem>
</Controls:MetroAnimatedTabControl>
I am setting the datacontext
in the TabItem
. I am creating a TcpCllient
and passing that reference to each ViewModel
. When I am doing this in the constructor everything works fine.
public MainViewModel()
{
_client = new TcpClient(IP_ADDRESS, PORT);
_client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
_master = ModbusIpMaster.CreateIp(_client);
ManualControlViewModel = new ManualControlViewModel(_master);
SetupViewModel = new SetupViewModel(_master);
}
However, I don't wish to create the TcpClient
in the constructor. Instead I wish to create it when I hit a "Connect" button in my window. However, when I click my "Connect" button to execute a OnConnect
method to do this my views are not getting set to the datacontext
.
private void OnConnect(object obj)
{
_client = new TcpClient(IP_ADDRESS, PORT);
_client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
_master = ModbusIpMaster.CreateIp(_client);
Connected = _client.Connected;
ManualControlViewModel = new ManualControlViewModel(_master);
SetupViewModel = new SetupViewModel(_master);
StartReadingInfo();
}
Is it possible to accomplish this?