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

#1 ScottinTexas  Icon User is offline

  • D.I.C Regular

Reputation: 8
  • View blog
  • Posts: 278
  • 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;
                Style="{StaticResource GroupBoxStyle}"
                Header="Test Instruments"
                Content="{Binding InstrumentList}"
                ContentTemplate="{StaticResource InstrumentViewTemplate}" />

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

        <DataTemplate x:Key="OperatorTemplate">
                <StackPanel Orientation="Vertical">
                        <Run Text="ID " />
                        <Run Text="{Binding ID}" />
                        <Run Text="-" />
                        <Run Text="{Binding Name}" />

        <TextBlock DockPanel.Dock="Left"
                   Style="{StaticResource TxtBlkStyle}">
            <Run Text="Inst.: " />
            <Run Text="{Binding InstrumentID, FallbackValue='X'}" />
        <TextBlock DockPanel.Dock="Left"
                   Style="{StaticResource TxtBlkStyle}">
                    <MenuItem Header="Associate USB" Command="{Binding AssociateComportCommand}" />
            <Run Text="Com:" />
            <Run Text="{Binding Comport, FallbackValue='COMX'}" />
        <TextBlock Style="{StaticResource TxtBlkStyle}">
            <Run Text="Analyst: " />
            ToolTip="Select an operator"
            SelectedItem="{Binding OperatorSelected}"
            ItemTemplate="{StaticResource OperatorTemplate}"
            ItemsSource="{Binding Operators}" />

In a resource file I have these resources.

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

    <DataTemplate x:Key="InstrumentTabTemplate">
        <Border BorderThickness="1"
                <Style TargetType="{x:Type Border}">
                        <DataTrigger Binding="{Binding HasHostages}" Value="True">
                            <Setter Property="BorderBrush" Value="Red"/>
                            <Setter Property="Background" Value="Red"/>
                Content="{Binding DisplayName}"
                VerticalAlignment="Center" >

    <DataTemplate x:Key="InstrumentViewTemplate">
          ItemTemplate="{StaticResource InstrumentTabTemplate}" 

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")
         private void HoldResultHostage(TestResult 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: 31
  • View blog
  • Posts: 388
  • 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: 8
  • View blog
  • Posts: 278
  • 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.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1