0

I have two inner views which I display conditionally as below

 <TabControl>
    <TabItem Header="TabItem" >
    <TabItem.Style>
        <Style TargetType="TabItem">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Condition}" Value="True">
                    <Setter Property="Content">
                        <Setter.Value>
                            <view:PaneView1 />
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding Condition}" Value="False">
                    <Setter Property="Content" >
                        <Setter.Value>
                                <view:PaneView2 />
                            </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TabItem.Style>                    
</TabItem>
</TabControl>

I now add DataContext in each view as below which is the property of my main ViewModel

<view:PaneView2 DataContext="{Binding PaneView2DC}"/>

The problem is that if I call PaneView2 with the datacontext without datatriggers it works fine but not inside the datatrigger. When I check the inner view's InitializeComponent, the Datacontext is null. Could someone please explain and help?

Jack Gray
  • 309
  • 2
  • 14
  • 1
    Are there any binding errors in the output window? – SnowballTwo Sep 09 '15 at 07:51
  • Yes there is an error if Datatrigger is used Cannot find element that provides DataContext. BindingExpression:Path=PaneView2DC; DataItem=null; target element is 'PaneView2' (Name=''); target property is 'DataContext' (type 'Object') – Jack Gray Sep 09 '15 at 07:56
  • Try the BindingProxy aproach from this post: http://stackoverflow.com/questions/22073740/binding-visibility-for-datagridcolumn-in-wpf – SnowballTwo Sep 09 '15 at 11:35
  • When the condition is false ,check whether MainViewModel is not null. – Nikita Shrivastava Sep 10 '15 at 10:28

0 Answers0