简体   繁体   中英

Binding data to XAML while runtime

I build a Chart with a Grid and some Rectangles, like in this Post: Drawing a chart in WPF C# design questions

Now I save this Chart to an XAML-File to fill it later by another program.

In this chart I need to bind three Values: 1. Labels 2. MaxHeight - Height (by each bar) 3. Height

My Problem is, that I don't know how to bind it correctly for using it in another application.

The other Application could give me a list or an array with the values and the label, but I dont know how I should bind unknown values to the Rectangles.

In the code below I binded the "ChartLabel" but I need all Chart Labels not only one. When I creating the chart I don't now how much bars I will have later.

  <Grid ShowGridLines="True" Background="#FFF5F5DC" Width="Auto" Margin="5,5,5,5" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
            <Rectangle Fill="#FF0000FF" Grid.Column="{Binding Path=Test6_Val, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
            <TextBox Text="{Binding Path=ChartLabel, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" BorderBrush="#00FFFFFF" Background="#00FFFFFF" HorizontalAlignment="Center" Grid.Column="5" Grid.Row="10" />
            <Rectangle Fill="#FF0000FF" Grid.Column="{Binding Path=Test7_Val, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
            <TextBox Text="{Binding Path=ChartLabel, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" BorderBrush="#00FFFFFF" Background="#00FFFFFF" HorizontalAlignment="Center" Grid.Column="6" Grid.Row="10" />
            <Rectangle Fill="#FF0000FF" Grid.Column="{Binding Path=Test8_Val, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
            <TextBox Text="{Binding Path=ChartLabel, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" BorderBrush="#00FFFFFF" Background="#00FFFFFF" HorizontalAlignment="Center" Grid.Column="7" Grid.Row="10" />
            <Rectangle Fill="#FF0000FF" Grid.Column="{Binding Path=Test9_Val, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
            <TextBox Text="{Binding Path=ChartLabel, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" BorderBrush="#00FFFFFF" Background="#00FFFFFF" HorizontalAlignment="Center" Grid.Column="8" Grid.Row="10" />
            <Rectangle Fill="#FF0000FF" Grid.Column="{Binding Path=Test10_Val, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
            <TextBox Text="{Binding Path=ChartLabel, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" BorderBrush="#00FFFFFF" Background="#00FFFFFF" HorizontalAlignment="Center" Grid.Column="9" Grid.Row="10" />
   </Grid>

I'm not sure I follow your description of which part you're having a problem with. You will have a List. You want to use a string from that as label and a number as height. If you iterate that you can find the max and hence calculate some sort of scaling factor so a value of half the max ends up about half the height of your chart. The way I'd do a series like that is with an itemscontrol. Bind an observablecollection to the itemssource. An itemscontrol has a stackpanel it's contents go in. Make the orientation horizontal and define an itemtemplate. That would have a textblock for the label, (maybe rotated) and a rectangle whose height is bound to your scaled value.

Which is roughly how the terrain visualiser I wrote works. There are no labels - so this is to give you the idea rather than cut and paste. 在此处输入图片说明

    <ListBox ItemsSource="{Binding TerrainPointList}"
             ..
             >
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel IsItemsHost="True" 
                                        Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>

...

A listbox has scrollers and selecteditem. I use selecteditem in mine and that might or might not be useful to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM