【问题标题】:How can I get the current datacontext from my control如何从我的控件中获取当前数据上下文
【发布时间】:2014-01-19 01:11:19
【问题描述】:

我有一个列表框,其中包含我正在尝试开发的控件选择器。基本上,数据源是 XML,我想读取当前上下文来决定显示哪个元素控件。

为此,我想使用当前项目的上下文获取 XmlDataProvider,并评估该 XML。在 XAML 中,我将编写 {Binding Path=@label} 以从 curretn XML 元素中检索标签属性。从后面的代码中,我什至无法确定从何处获取此 XML,因为它是由列表控件传递给类的,但据我所知,它不是可访问的属性。

无论如何获得@label 是不够的;我想要 ControlChooser 类中的 XmlElement 对象,在下面实例化。

 <ListBox 
  IsSynchronizedWithCurrentItem="True"
  ItemsSource="{Binding XPath=*[not(self::units)]}"
  >
  <ListBox.ItemTemplate>
    <DataTemplate>
      <W3V:ControlChooser/>
    </DataTemplate>
  </ListBox.ItemTemplate>
  <ListBox.ItemContainerStyle>
    <!-- Force the items to fill all available space. -->
    <Style TargetType="ListBoxItem">
      <Setter 
        Property="VerticalContentAlignment" 
        Value="Stretch" 
        />
    </Style>
  </ListBox.ItemContainerStyle>
</ListBox>

或者,如果您可以建议另一种方法来完成这项工作(切换显示的控件)......

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    您可以使用多个DataTemplates 来设置不同元素类型的样式。如果您熟悉 XSLT,DataTemplates 在功能上等同于 xsl:template

    样式示例:

    <Window x:Class="ZoomingScrollViewer.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <XmlDataProvider x:Key="testData" XPath="/Contacts/*">
                <x:XData>
                    <Contacts xmlns="">
                        <Person Name="John" />
                        <Person Name="Robby" />
                        <Business>
                            <ContactName>Jemma</ContactName>
                            <BusinessName>Ars</BusinessName>
                        </Business>
                        <Business>
                            <BusinessName>The other one</BusinessName>
                        </Business>
                    </Contacts>
                </x:XData>
            </XmlDataProvider>
        </Window.Resources>
        <Grid>
            <ListBox ItemsSource="{Binding Source={StaticResource testData}}">
                <ListBox.Resources>
                    <DataTemplate DataType="Person">
                        <TextBlock Text="{Binding XPath=@PersonName}" />
                    </DataTemplate>
                    <DataTemplate DataType="Business">
                        <StackPanel>
                            <TextBlock Text="{Binding XPath=ContactName}" />
                            <TextBlock Text="{Binding XPath=BusinessName}" />
                        </StackPanel>
                    </DataTemplate>
                </ListBox.Resources>
            </ListBox>
        </Grid>
    </Window>
    

    如果您想根据属性而不是元素选择DataTemplate,您可以使用DataTemplateSelector 来运行任意代码,如this question 中所述。

    【讨论】:

    • 我想我需要那个 DataTemplateSelector。感谢您的链接。 (或者也许我应该调整 XML 数据以获得正确的内容。
    • 当然,如果这解决了您的问题,您可以将其标记为已回答。
    【解决方案2】:

    我发现“Different views / data template based on member variable”最接近答案。有了这个想法,我可以制作一个对我有意义的独立控件。这是关键:

      <DataTrigger Binding="{Binding XPath=@kind}" Value="Number">
         <Setter Property="ContentTemplate" Value="{StaticResource SmallInt}" />
      </DataTrigger>
    

    这是整个控件:

    <ContentControl
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:W3V="clr-namespace:W3.Views"
                 x:Class="W3.Views.ControlChooser">
      <ContentControl.Resources>
        <DataTemplate x:Key="StringChoice" >
          <W3V:ComboView />
        </DataTemplate>
    
        <DataTemplate x:Key="SmallInt" >
          <W3V:SpinView />
        </DataTemplate>
    
      </ContentControl.Resources>
      <ContentControl.Style>
          <Style TargetType="{x:Type ContentControl}">
            <Setter Property="ContentTemplate" Value="{StaticResource StringChoice}" />
            <Style.Triggers>
              <DataTrigger Binding="{Binding XPath=@kind}" Value="Number">
                <Setter Property="ContentTemplate" Value="{StaticResource SmallInt}" />
              </DataTrigger>
            </Style.Triggers>
          </Style>
        </ContentControl.Style>
    </ContentControl>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-13
      • 1970-01-01
      • 1970-01-01
      • 2020-04-22
      • 1970-01-01
      • 2017-06-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多