M
M
Mikhail Usotsky2016-08-08 13:48:56
WPF
Mikhail Usotsky, 2016-08-08 13:48:56

How to track changing data?

There is a class that has a tab title and panel content (TabControl).

public class TabInfo
    {
        public string Header { get; set; }
        public Panel Content { get; set; }
    }

This class is bound to tabs via an ObservableCollection.
Here in the contents of the panel (Panel) there are two string entries that are updated every second. I have many tabs. But I need to make the entries in the StatusBar display the information of two string entries from the active content.
<Window x:Class="ExperiencePanelsWPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ExperiencePanelsWPF"
        mc:Ignorable="d"
        Title="MainWindow" Height="1024" Width="1280" Background="White" WindowStartupLocation="CenterScreen" WindowState="Maximized">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid.Resources>
            <Style TargetType="{x:Type MenuItem}">
                <Setter Property="Padding" Value="12,8"/>
            </Style>
        </Grid.Resources>
        <Menu Grid.Row="0" Background="White">
            <MenuItem Header="Устройства">
                <MenuItem Header="Добавить устройство" Click="AddTab"/>
                <MenuItem Header="Закрыть устройство (не работает)"/>
            </MenuItem>
            <MenuItem Header="Дополнительные опции для устройства">
                <MenuItem Header="Загрузка файла на десериалайзер" Click="LoadFileForGTX"/>
                <MenuItem Header="Работа с Flash-накопителем"/>
                <MenuItem Header="Общий сброс" Name="GlobalReset"/>
            </MenuItem>
            <MenuItem Header="Проверка элементов (тестирование)">
                <MenuItem Header="Добавить элемент с особым значением (тестирование)" Click="SpecialAdd"/>
                <MenuItem Header="Проверка элемента (тестирование)" Click="CheckElement"/>
                <MenuItem Header="Присвоить значение элемента (тестирование)" Click="CheckSet"/>
            </MenuItem>
        </Menu>
        <Grid Grid.Row="1">
            <TabControl Grid.Row="1" ItemsSource="{Binding ListGTP}" SlectedItem="{Binding SelectedTab}" Name="TabGTP" Background="#FFF9F9F9" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" SelectionChanged="DeviceChanged">
                <TabControl.ContentTemplate>
                    <DataTemplate>
                        <ContentControl Content="{Binding Path=Content, Mode=TwoWay}"/>
                    </DataTemplate>
                </TabControl.ContentTemplate>
                <TabControl.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=Header}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                    </DataTemplate>
                </TabControl.ItemTemplate>
            </TabControl>
        </Grid>
        <StatusBar Grid.Row="2" Background="#FF007ACC" Foreground="White">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="80"/>
                    <ColumnDefinition Width="180"/>
                    <ColumnDefinition Width="80"/>
                    <ColumnDefinition Width="128"/>
                    <ColumnDefinition Width="210"/>
                    <ColumnDefinition Width="32"/>
                    <ColumnDefinition Width="64"/>
                    <ColumnDefinition Width="210"/>
                    <ColumnDefinition Width="256"/>
                    <ColumnDefinition Width="210"/>
                    <ColumnDefinition Width="128"/>
                    <ColumnDefinition/>
                    <ColumnDefinition/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
                <StatusBarItem Grid.Column="0" Content="Устройство: " HorizontalContentAlignment="Right"/>
                <StatusBarItem Grid.Column="1" Content="" Name="SelectedDevice"/>
                <StatusBarItem Grid.Column="2" Content="Состояние: " HorizontalContentAlignment="Right"/>
                <StatusBarItem Grid.Column="3" Content="" Name="StatusDevice"/>
                <StatusBarItem Grid.Column="4" Content="Количество запущенных устройств: " HorizontalContentAlignment="Right"/>
                <StatusBarItem Grid.Column="5" Content="" Name="CountDevice"/>
                <StatusBarItem Grid.Column="6" Content="###"/>
                <StatusBarItem Grid.Column="7" Content="Скорость приёма данных от ПЛИС: " HorizontalContentAlignment="Right"/>
                <StatusBarItem Grid.Column="8" Name="SpeedLoadingPC" DataContext="{Binding SelectedTab}" Content="{Binding Content.TestSpeedLoadingPC}"/>
                <StatusBarItem Grid.Column="9" Content="Скорость передачи данных к ПЛИС: " HorizontalContentAlignment="Right"/>
                <StatusBarItem Grid.Column="10" Name="SpeedLoadingFPGA" Content="###"/>
            </Grid>
        </StatusBar>
    </Grid>
</Window>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stanislav Silin, 2016-08-08
@Aquarius-Michael

Add a variable to the view model (which contains the collection) and bind SelectedItem to it. Well, then we bind the status bar to it.
You can also do something similar in xaml using ElementName , which will refer to the "tabco container" and pull the SelectedItem out of it.
Not really, there are two options, the first one is through the view model:
1. Add the SelectedTab field to the view model.
2. Bindim to it I will select a tab
3. Bindim the status bar

...
<StatusBarItem>
   <TextBlock DataContext="{Binding SelectedTab}" Text="{Binding FirstField}" />
</StatusBarItem>
<Separator Grid.Column="1" />
<StatusBarItem Grid.Column="2">
   <TextBlock DataContext="{Binding SelectedTab}" Text="{Binding SecondField}" />
 </StatusBarItem>
...

The second way ElementName
1. Give a name to the "tabocontainer"
2. Bind to it
...
<StatusBarItem>
    <TextBlock DataContext="{Binding ElementName=tabControl, Path=SelectedItem}" Text="{Binding FirstField}" />
</StatusBarItem>
<Separator Grid.Column="1" />
<StatusBarItem Grid.Column="2">
   <TextBlock DataContext="{Binding ElementName=tabControl, Path=SelectedItem}" Text="{Binding SecondField}" />
</StatusBarItem>
...

And yes, you will need INotifyPropertyChanged here. The first option is preferable because the logical relationship is established at the view model level, and not in the presentation.

M
Mikhail Usotsky, 2016-08-12
@Aquarius-Michael

Thanks to everyone who helped to resolve the issue. It turns out that for this it is necessary to subscribe to the event when initializing the object in order to collect data when the event occurs .. Then it will work.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question