将多个源绑定到 ListView

发布于 2024-10-30 04:13:56 字数 1159 浏览 1 评论 0原文

我有一个带有 dataTemplate 的 ListView,我需要将其绑定到具有相同索引的 3 个不同源。我认为我必须完全在 XAML 中完成此操作,因为源(chart)仅存在于 xaml 中。我正在使用 MVVM 模式。”
我已经写下了它“应该”如何工作,索引i是公共密钥。

<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=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>

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=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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赠意 2024-11-06 04:13:56

嗯,让我们看看。如何将列表视图绑定到 chart.Series 这样您就可以获得正确数量的元素。然后在数据模板中绑定该系列的属性。对于您可以使用 MultiBinding 和转换器来提取数据的行为

<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>

现在您应该将图表和当前系列对象传递到您的转换器中,您应该能够在其中执行以下操作类似 var idx = Chart.Series.IndexOf(s) 这样您就可以访问行为中的相应点。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文