【问题标题】:Binding with an appropriate UIElement for ObservableDictionary Silverlight为 ObservableDictionary Silverlight 绑定适当的 UIElement
【发布时间】:2014-12-16 19:14:19
【问题描述】:

我在声明用于显示 KeyValuePair 列表或网格的 DataTemplate 时遇到问题。当 ContentTemplate 声明如下时,我正在后面的代码中设置以下控件的 Content 属性。

        <esri:InfoWindow x:Name="MyInfoWindow" IsOpen="False"
                     Padding="2"
                     CornerRadius="0" 
                     Background="White"                         
                     ContentTemplate="{StaticResource MyFeatureLayerInfoWindowTemplate}"
                     Map="{Binding ElementName=MyMap}" Content="Something">
            <esri:InfoWindow.ContentTemplate>
                <DataTemplate x:Key="MyFeatureLayerInfoWindowTemplate">
                    <sdk:DataGrid>
                        <sdk:DataGrid.Template>
                            <ControlTemplate>
                                <TextBlock Text="{Binding}" Foreground="Black" FontSize="12" />
                            </ControlTemplate>
                        </sdk:DataGrid.Template>
                    </sdk:DataGrid>
                </DataTemplate>
            </esri:InfoWindow.ContentTemplate>
        </esri:InfoWindow>

ControlTemplate 中的 Textblock 可以很好地接收 Content 中设置的对象,但它应该显示接收到的对象的类型。

我放入 DataGrid 的原因是,如果我将绑定如下所示,它将以网格格式显示集合(只是关键属性),但如果我写这个,输出为空。

<TextBlock Text="{Binding Path=Key}" Foreground="Black" FontSize="12" />

【问题讨论】:

    标签: c# wpf silverlight data-binding


    【解决方案1】:

    如您所见,ObservableDictionary 本身正在传递给该模板,然后您可以将该字典传递给某种类型的 ItemsControl。

    里面...

    <sdk:DataGrid.Template>
        <ControlTemplate>
           <TextBlock Text="{Binding}" Foreground="Black" FontSize="12" />
        </ControlTemplate>
    </sdk:DataGrid.Template>
    

    尝试类似...

    <sdk:DataGrid.Template>
       <ControlTemplate>
          <ItemsControl ItemsSource="{Binding}">
             <ItemsControl.ItemTemplate>
                <DataTemplate>
                   <TextBlock Text="{Binding Path=Key}" Foreground="Black" FontSize="12" />
                </DataTemplate>
             </TtemsControl.ItemTemplate>
          </ItemsControl>
        </ControlTemplate>
    </sdk:DataGrid.Template>
    

    看看它会给你什么。

    【讨论】:

    • 现在我看到了,我在想我是多么白痴才能错过这样的事情:)
    • 不客气,塔哈。很高兴能提供帮助。
    猜你喜欢
    • 1970-01-01
    • 2015-08-10
    • 2011-04-19
    • 2012-01-12
    • 1970-01-01
    • 2013-03-30
    • 1970-01-01
    • 2015-03-10
    • 1970-01-01
    相关资源
    最近更新 更多