开发者

Binding multiple Sources to a ListView

I have a ListView with a dataTemplate which I need to bind to 3 different sources with the same index. I think I have to do this completely in XAML, because the sources (chart) only exists in xaml. I'm using the MVVM Pattern."

I have wrote down how it "should" work, the index i is the common key.

<ListView ItemsSource="{Binding ???}">
   <ListView.ItemTemplate>
      <DataTemplate>
         <StackPanel>
             <!-- Small rectangle filled with the same color as the corresponding line -->
            <Rectangle 
              Height="10"
              Width="10" 
              Fill="{Binding ElementName=chart, Path=Series[i].LineStroke}" />
            <!-- The title of the corresponding line -->
            <TextBlock
              x:Name="Title"
              Text="{Binding ElementName=chart, Path=开发者_StackOverflow社区Series[i].DataSeries.Title}" />
            <!-- The actual value of the corresponding line on the current position-->
            <TextBlock
              x:Name="Value"
              Text="{Binding ElementName=chart, Path=Behaviour.Behaviours[0].CurrentPoints[i].Y}" />
        </StackPanel>
      </DataTemplate>
   </ListView.ItemTemplate>
</ListView>


Mh, lets see. How about you bind you listview to chart.Series this way you get the right number of elements. An then in your data template you bind the properties of the series. For the behaviour you could use MultiBinding and a converter to extract the data

<ListView ItemsSource="{Binding Path=Series, ElementName=chart}">
 <ListView.ItemTemplate>
  <DataTemplate>
     <StackPanel>
         <!-- Small rectangle filled with the same color as the corresponding line -->
        <Rectangle 
          Height="10"
          Width="10" 
          Fill="{Binding Path=LineStroke}" />
        <!-- The title of the corresponding line -->
        <TextBlock
          x:Name="Title"
          Text="{Binding Path=DataSeries.Title}" />
        <!-- The actual value of the corresponding line on the current position-->
        <TextBlock x:Name="Value">
          <TextBlock.Text>
              <MultiBinding Converter="{StaticResource ChartSeriesBehaviourConverter}">
                 <Binding ElementName=chart/>
                 <Binding/>
              <MultiBinding>
        </TextBlock>
    </StackPanel>
  </DataTemplate>
 </ListView.ItemTemplate>
</ListView>

Now you should get the chart and the current series object passed into your converter where you should be able to do something like var idx = chart.Series.IndexOf(s) so you can access the corresponding point in the behaviours.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜