Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Getting the value of Height through OneWayToSource binding

Getting the value of Height through OneWayToSource binding

Scheduled Pinned Locked Moved WPF
wpfcsharpcsswcfdesign
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    la01
    wrote on last edited by
    #1

    I have a ListBox whose Height is bound one way to source. As you can see from the XAML below, the listbox is supposed to take up all the space available to it, and it's height is not initialised to anything in C# code.

    <Grid>
    <Grid.RowDefinitions>
    <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <ListBox ItemsSource="{Binding PhotoList}" Grid.Row="0" Height="{Binding Path=listBoxH, Mode=OneWayToSource}">

    </ListBox>
    

    </Grid>

    My problem is that I need to get a numerical value for listBoxH in the code behind (bounded code, it's actually the ViewModel of my UI). But this value is double.NaN. This is expected as the Height/listBoxH is not "set" anywhere, but is there a way to get the ActualHeight of the ListBox? I need to get the current height of the ListBox, use it do a calculation which decides what to display in the ListBox. I can't directly reference the ListBox in C# code as it doesn't belong to the same class, and this is not the approach I want to take. Workarounds, anyone?

    A 1 Reply Last reply
    0
    • L la01

      I have a ListBox whose Height is bound one way to source. As you can see from the XAML below, the listbox is supposed to take up all the space available to it, and it's height is not initialised to anything in C# code.

      <Grid>
      <Grid.RowDefinitions>
      <RowDefinition Height="*" />
      </Grid.RowDefinitions>
      <ListBox ItemsSource="{Binding PhotoList}" Grid.Row="0" Height="{Binding Path=listBoxH, Mode=OneWayToSource}">

      </ListBox>
      

      </Grid>

      My problem is that I need to get a numerical value for listBoxH in the code behind (bounded code, it's actually the ViewModel of my UI). But this value is double.NaN. This is expected as the Height/listBoxH is not "set" anywhere, but is there a way to get the ActualHeight of the ListBox? I need to get the current height of the ListBox, use it do a calculation which decides what to display in the ListBox. I can't directly reference the ListBox in C# code as it doesn't belong to the same class, and this is not the approach I want to take. Workarounds, anyone?

      A Offline
      A Offline
      AspDotNetDev
      wrote on last edited by
      #2

      Most (all?) controls that have a Height also have an ActualHeight property. Not sure if you can bind to it. You might be able to use a size change event to detect when the size has changed then then get the value of ActualHeight.

      [Forum Guidelines]

      L 1 Reply Last reply
      0
      • A AspDotNetDev

        Most (all?) controls that have a Height also have an ActualHeight property. Not sure if you can bind to it. You might be able to use a size change event to detect when the size has changed then then get the value of ActualHeight.

        [Forum Guidelines]

        L Offline
        L Offline
        la01
        wrote on last edited by
        #3

        Thanks for the tip. I found that changes to ActualHeight could be used as a trigger. I've tried to use an event trigger, and am currently stuck. Here's what I've done:

        <Grid>
        <Grid.RowDefinitions>
        <RowDefinition Height="{Binding gridHeight}" />
        </Grid.RowDefinitions>
        <ListBox ItemsSource="{Binding PhotoList}" Grid.Row="0" >
        <ListBox.Style>
        <Style>
        <Style.Triggers>
        <EventTrigger RoutedEvent="SizeChanged">
        <!-- Set {Binding tabHeight} to ActualHeight here -->
        </EventTrigger>
        </Style.Triggers>
        </Style>
        </ListBox.Style>
        </ListBox>
        </Grid>

        The problem is, I can't figure out how to tell it to update tabHeight once the event is triggered. I initially thought I could use a Setter to update {Binding Path=tabHeight}. But that doesn't seem to be the case. Suggestions are much appreciated :)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups