.NET Framework Developer Center > .NET Development Forums > Windows Presentation Foundation (WPF) > Can I use ObjectDataProvider.ObjectInstance in a DataTrigger?
Ask a questionAsk a question
 

AnswerCan I use ObjectDataProvider.ObjectInstance in a DataTrigger?

  • Wednesday, April 11, 2007 8:28 AMmabster Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi guys,

     

    I have a Grid which I want hidden until I assign a value to an ObjectDataProvider's ObjectInstance property.

     

    The idea is that the grid is hidden until the user clicks "File|New" or "File|Open", at which time a new object is instantiated and assigned to my ObjectDataProvider.

     

    I've tried this sort of thing:

     

    Code Snippet

    <Grid Visibility="Hidden">

       <Grid.Triggers>

          <DataTrigger Binding="{Binding Source={StaticResource collectionData},Path=ObjectInstance}">

             <Setter Property="Visibility" Value="Visible" />

          </DataTrigger>

       </Grid.Triggers>

    </Grid>

     

     

     

    ... but this just crashes the app when it runs. A "XAML Parse Error".

     

    Am I on the right track? Any pointers as to how I could accomplish this short of setting the property in the code-behind when the object is assigned?

     

    Cheers,

    Matt

Answers

  • Friday, April 13, 2007 1:32 PMJosh SmithMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    There are several things wrong with your approach.

    1) You didn't specify a Value for the DataTrigger

    2) Elements can only have EventTriggers in v1 of WPF.  The folks at MSFT didn't have time to implement support for other trigger types for elements.

    3) You are not binding directly to the ObjectDataProvider, which means that you can't bind against the ObjectInstance property.

     

    Here's a technique that worked for me:

     

    Code Snippet

    <StackPanel>

      <StackPanel.Resources>

     

        <ObjectDataProvider x:Key="dataProv" />

     

        <Style x:Key="GridStyle" TargetType="Grid">

          <Style.Triggers>

            <DataTrigger

              Binding="{Binding Source={StaticResource dataProv}, Path=ObjectInstance, BindsDirectlyToSource=True}"

              Value="{x:Null}"

              >

              <Setter Property="Visibility" Value="Hidden"/>

            </DataTrigger>

          </Style.Triggers>

        </Style>

      </StackPanel.Resources>

     

      <Button>Click Here To Create An ObjectInstance</Button>

     

      <Grid Style="{StaticResource GridStyle}">

        <TextBlock Background="Red">An object exists!</TextBlock>

      </Grid>

    </StackPanel>

     

    In the Button's Click handler I assigned a value to dataProv's ObjectInstance property, and that caused the Grid to become Visible.

All Replies

  • Friday, April 13, 2007 9:16 AMYi-Lun LuoMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hello, I think you can simply write this in the menu’s event handler:

    grid1.Visibility = Visibility.Visible;

  • Friday, April 13, 2007 9:19 AMmabster Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
     Yi-Lun Luo - MSFT wrote:

    Hello, I think you can simply write this in the menu’s event handler:

    grid1.Visibility = Visibility.Visible;

     

    Yeah, I'm doing it in the code-behind at the moment. I was just wondering whether it's possible to do declaratively in the XAML.

  • Friday, April 13, 2007 1:32 PMJosh SmithMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    There are several things wrong with your approach.

    1) You didn't specify a Value for the DataTrigger

    2) Elements can only have EventTriggers in v1 of WPF.  The folks at MSFT didn't have time to implement support for other trigger types for elements.

    3) You are not binding directly to the ObjectDataProvider, which means that you can't bind against the ObjectInstance property.

     

    Here's a technique that worked for me:

     

    Code Snippet

    <StackPanel>

      <StackPanel.Resources>

     

        <ObjectDataProvider x:Key="dataProv" />

     

        <Style x:Key="GridStyle" TargetType="Grid">

          <Style.Triggers>

            <DataTrigger

              Binding="{Binding Source={StaticResource dataProv}, Path=ObjectInstance, BindsDirectlyToSource=True}"

              Value="{x:Null}"

              >

              <Setter Property="Visibility" Value="Hidden"/>

            </DataTrigger>

          </Style.Triggers>

        </Style>

      </StackPanel.Resources>

     

      <Button>Click Here To Create An ObjectInstance</Button>

     

      <Grid Style="{StaticResource GridStyle}">

        <TextBlock Background="Red">An object exists!</TextBlock>

      </Grid>

    </StackPanel>

     

    In the Button's Click handler I assigned a value to dataProv's ObjectInstance property, and that caused the Grid to become Visible.

  • Friday, April 13, 2007 11:36 PMmabster Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Awesome! Thanks Josh, I'll give it a go.

     

    I'm very new to this, and I'm kind of using the "type in XAML and rearrange it until it works" approach until I can be bothered buying a book etc. Smile

  • Friday, April 13, 2007 11:42 PMJosh SmithMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Mabster, If I answered your question then you should mark my reply as the Answer, so that others will know that this thread contains useful info.  Wink

     

    BTW - I blogged about this here: http://joshsmithonwpf.wordpress.com/2007/04/13/bindsdirectlytosource/