Note that there are some explanatory texts on larger screens.

plurals
  1. POTabControl: Current Content in selected Tabs MVVM
    primarykey
    data
    text
    <p>I've got a problem: my professor wrote a program where we have to add some features. There's a WPF with a textbox and a button, and when clicking the button the content of the textbox is sent to the client via <code>MessageQueue</code>.</p> <p>I have to create a new WPF, where the output of the messages should be visible. I've arranged that, but there's another feature: we have to generate a dynamical tab system, where tabs can be added and removed (like in Firefox e.g.). The sent messages should be visible in the opened tab only! Example: I've got 2 tabs (tab1 and tab2) and tab1 is open: I write "text1" into the textbox and click the button, afterwards I open tab2 and I write "text2" into the textbox and click the button. In tab1 i should only see "text1" and in tab2 I should only see "text2".</p> <p>I don't know how to arrange this with MVVM. Using code behind would be easy: <code>myTabs.SelectedIndex.Items.CurrentItem.Text="blabla";</code> - but we have to do it with MVVM.</p> <p>I would be really happy if you could help me! Thanks</p> <pre><code>public class MainViewModels:INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private ObservableCollection&lt;AgentModel&gt; _agents; public ObservableCollection&lt;AgentModel&gt; Agents { get { if (_agents == null) { _agents=new ObservableCollection&lt;AgentModel&gt;(); } return _agents; } set { _agents = value; } } public AgentModel SelectedAgent { get { return _selectedAgent; } set { _selectedAgent = value; if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("SelectedAgent")); } } private AgentModel _selectedAgent; private LogicHandler _lh; private Dispatcher _maindispatcher = Dispatcher.CurrentDispatcher; public MainViewModels() { Agents = new ObservableCollection&lt;AgentModel&gt;(); if (!DesignerProperties.GetIsInDesignMode(new DependencyObject())) { ConnectToMsmq(); } } private void ConnectToMsmq() { _lh = new LogicHandler(new MessageInformer(NewMessageReceived)); } private void NewMessageReceived(CoreMessage message) { _maindispatcher.Invoke(() =&gt; { var agent = Agents.FirstOrDefault(x =&gt; x.Host == message.Source); //foreach(var x in Agents) if (agent == null) { agent = new AgentModel { Host = message.Source, Name = message.Source, Time = message.Date, }; Agents.Add(agent); } agent.Messages.Add(message.Data); }); } </code></pre> <p>......</p> <pre><code>&lt;Window x:Class="CoreOutputWpf.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:vm="clr-namespace:CoreOutputWpf.Viewmodels" Title="MainWindow" Height="350" Width="800"&gt; &lt;Window.DataContext&gt; &lt;vm:MainViewModels/&gt; &lt;/Window.DataContext&gt; &lt;Grid&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="0.7*"/&gt; &lt;ColumnDefinition Width="0.3*"/&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;Canvas Grid.Column="0"&gt; &lt;TabControl Margin="20,0,0,0" Name="mainTabs"&gt; &lt;TabItem x:Name="tab0" Width="60" Selector.IsSelected="True" Header="tab0" /&gt; &lt;/TabControl&gt; &lt;Button Name="btnplus" Content="+" Click="btnplus_Click_1" /&gt; &lt;x:Code&gt; private void btnplus_Click_1(object sender, RoutedEventArgs e) { int numb = mainTabs.Items.Count; numb++; mainTabs.Items.Add("tab "+numb+""); tabi.Text = mainTabs.SelectedIndex.ToString(); } &lt;/x:Code&gt; &lt;StackPanel Margin="20,50,0,0"&gt; &lt;TextBlock Text="{Binding SelectedAgent.Host}"/&gt; &lt;TextBlock Text="{Binding SelectedAgent.Time}"/&gt; &lt;ListBox ItemsSource="{Binding SelectedAgent.Messages}"/&gt; &lt;TextBlock Name="tabi"/&gt; &lt;/StackPanel&gt; &lt;/Canvas&gt; &lt;Grid Grid.Column="1"&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="Auto"/&gt; &lt;RowDefinition Height="Auto"/&gt; &lt;RowDefinition Height="*"/&gt; &lt;/Grid.RowDefinitions&gt; &lt;Border Grid.Row="0" Background="SteelBlue"&gt; &lt;TextBlock HorizontalAlignment="Center" Margin="10" FontSize="30" Text="Agents"/&gt; &lt;/Border&gt; &lt;TextBox Grid.Row="1" Margin="5"/&gt; &lt;DataGrid Grid.Row="2" Margin="5" ItemsSource="{Binding Agents}" SelectedItem="{Binding SelectedAgent}"/&gt; &lt;/Grid&gt; &lt;/Grid&gt; </code></pre> <p></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload