2 Replies - 397 Views - Last Post: 21 September 2017 - 06:09 AM Rate Topic: -----

#1 ScottinTexas  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 261
  • Joined: 13-March 12

Binding a Tab Item to a Viewmodel Property

Posted 18 September 2017 - 01:37 PM

I don't know what I have wrong here. I want the tab in the tab control to turn red when the property HasHostages is true.

On the MainWIndowView;
            <GroupBox
                Style="{StaticResource GroupBoxStyle}"
                Grid.Row="0"
                Grid.Column="0"
                Header="Test Instruments"
                Content="{Binding InstrumentList}"
                ContentTemplate="{StaticResource InstrumentViewTemplate}" />




The InstrumentView is simply a UserControl;
   <UserControl.Resources>
        <Style TargetType="TextBlock"
               x:Key="TxtBlkStyle">
            <Setter Property="VerticalAlignment" Value="Center" />
            <Setter Property="FontFamily" Value="Verdana" />
            <Setter Property="FontSize" Value="12" />
            <Setter Property="Margin" Value="4" />
        </Style>

        <DataTemplate x:Key="OperatorTemplate">
            <Border
            BorderBrush="Gray"
            BorderThickness="1">
                <StackPanel Orientation="Vertical">
                    <TextBlock>
                        <Run Text="ID " />
                        <Run Text="{Binding ID}" />
                        <Run Text="-" />
                        <Run Text="{Binding Name}" />
                    </TextBlock>
                </StackPanel>
            </Border>
        </DataTemplate>
    </UserControl.Resources>

    <DockPanel
        Background="Beige">
        <TextBlock DockPanel.Dock="Left"
                   Style="{StaticResource TxtBlkStyle}">
            <Run Text="Inst.: " />
            <Run Text="{Binding InstrumentID, FallbackValue='X'}" />
        </TextBlock>
        <TextBlock DockPanel.Dock="Left"
                   Style="{StaticResource TxtBlkStyle}">
            <TextBlock.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Associate USB" Command="{Binding AssociateComportCommand}" />
                </ContextMenu>
            </TextBlock.ContextMenu>
            <Run Text="Com:" />
            <Run Text="{Binding Comport, FallbackValue='COMX'}" />
        </TextBlock>
        <TextBlock Style="{StaticResource TxtBlkStyle}">
            <Run Text="Analyst: " />
        </TextBlock>
        <ComboBox
            DockPanel.Dock="Right"
            Background="Beige"
            BorderBrush="Transparent"
            VerticalAlignment="Center"
            HorizontalAlignment="Stretch"
            HorizontalContentAlignment="Stretch"
            SelectedIndex="0"
            ToolTip="Select an operator"
            SelectedItem="{Binding OperatorSelected}"
            ItemTemplate="{StaticResource OperatorTemplate}"
            ItemsSource="{Binding Operators}" />
    </DockPanel>



In a resource file I have these resources.

    <DataTemplate DataType="{x:Type vm:InstrumentViewModel}">
        <vw:InstrumentView />
    </DataTemplate>

    <DataTemplate x:Key="InstrumentTabTemplate">
        <Border BorderThickness="1"
                CornerRadius="3"
                BorderBrush="Green"
                Background="#FFFFFBE2"
                Margin="3">
            <Border.Style>
                <Style TargetType="{x:Type Border}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding HasHostages}" Value="True">
                            <Setter Property="BorderBrush" Value="Red"/>
                            <Setter Property="Background" Value="Red"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Border.Style>
            <ContentPresenter
                Content="{Binding DisplayName}"
                VerticalAlignment="Center" >
            </ContentPresenter>
        </Border>
        
    </DataTemplate>

    <DataTemplate x:Key="InstrumentViewTemplate">
        <TabControl
          IsSynchronizedWithCurrentItem="True"
          ItemsSource="{Binding}"
          ItemTemplate="{StaticResource InstrumentTabTemplate}" 
          Margin="5" 
          Background="#FFFFFBE2"  
          />
    </DataTemplate>



So an observable collection of InstrumentViewModels are created at the start of the program. During operation the Instrument may get hostages. In the ViewModel I set the HasHostages boolean to true. This also raises the property changed event. So the view should reflect the change, but I don't see it.

During operation some data comes in on the USB port. I collect it and do things with it. I also check to see if the operator for that instrument has been selected.
        if (OperatorName == "" || OperatorName == "Select Operator")
            HoldResultHostage(result);
        
         private void HoldResultHostage(TestResult result)
        {
            Hostages.Add(result);
            var obj=instrumentList.FirstOrDefault(x => x.InstrumentID == result.Instrument);
            obj.HasHostages = true;
        }   
 




As a test I just set the HasHostages to true in the ctor and stepped through the startup. All of the instruments had hostages, but none of the tabs were red.

Is This A Good Question/Topic? 0
  • +

Replies To: Binding a Tab Item to a Viewmodel Property

#2 JapanDave  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 365
  • Joined: 01-February 16

Re: Binding a Tab Item to a Viewmodel Property

Posted 19 September 2017 - 04:50 AM

I can't understand what you have done completely, but looks like you should be making HasHostages a static property in your ViewModel.

This post has been edited by JapanDave: 19 September 2017 - 04:52 AM

Was This Post Helpful? 0
  • +
  • -

#3 ScottinTexas  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 261
  • Joined: 13-March 12

Re: Binding a Tab Item to a Viewmodel Property

Posted 21 September 2017 - 06:09 AM

Dave, I figured it out. The Instrument model did not have the property "HasHostages." In my InstrumentViewModel I was accessing the property "HasHostages" which was fine but it needed to be on the instance of the instrument (instrument.HasHostages). I didn't mean for it to get so complicated. It just developed that way as I went. Maybe if I had whiteboarded the whole idea at first it would have been more streamlined. It is simply this. I create four objects of InstrumetnViewModel. Each one has a property of type Instrument as well as the necessary methods to handle list selection etc. Each of these ViewModel objects are added to a collection in the MainWindowViewModel. The view has a ListView bound to that collection. So what happens is data arrives on the com port. It is folded, spindled and mutilated in the methods in the background worker and passed back out to the MainWindowViewModel. There it is associated with an instrument based on the Com port (instrument 1 is on Com 14 for example). If the operator for the instrument has not been selected then the data is held hostage until that little piece is satisfied. Now I have to subscribe to the property changed event to release the hostages. Please see my next post.

Thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1