在ItemsSource中使用公共属性填充ItemsControl,而无需添加代码 [英] Populating ItemsControl with public properties in ItemsSource without code behind

查看:51
本文介绍了在ItemsSource中使用公共属性填充ItemsControl,而无需添加代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以仅使用XAML使用DataContext的公共属性填充ItemsControl或其任何扩展?

Is it possible to populate an ItemsControl, or any of its extensions, with the public properties of the DataContext using only XAML?

问候,

Kim

推荐答案


祝你好运,


Hi klillas,

我不太了解您的问题.据我所知,当DataContext包含公共属性时,我们可以在XAML中使用它( 绑定路径= DataContext.publicproperties ).

I am not very aware of your problem. As far as I know, when the DataContext contains the public properties, we can use it in XAML ( Binding Path=DataContext.publicproperties ).

以下代码供您参考.

  <Window.Resources>
        <ControlTemplate x:Key="WithPopup" TargetType="ContentControl">
            <Grid Name="popupGrid" >
                <TextBlock Text="{Binding }"  />
                <Popup x:Name="MyPopup" Width="160" Height="26"
                       PlacementTarget="{Binding ElementName=popupGrid}">
                    <Popup.Style>
                        <Style TargetType="Popup">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding PlacementTarget.IsMouseOver, RelativeSource={RelativeSource Self}}" 
                                             Value="True">
                                    <Setter Property="IsOpen" Value="True" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Popup.Style>
                    <Border x:Name="testboder" BorderBrush="Red" BorderThickness="1" Background="Pink" >
                        <TextBlock Text="{Binding }"/>
                    </Border>
                </Popup>
            </Grid>
        </ControlTemplate>

    </Window.Resources>
    <Grid Width="190" HorizontalAlignment="Left" Margin="50,50,0,87">
        <StackPanel Margin="10,10,15,32">
            <ComboBox x:Name="combx" ItemsSource="{Binding Path=MyItems}" SelectedItem="{Binding Path=MySelectedItem}" SelectionChanged="combx_SelectionChanged">
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <ContentControl Template="{StaticResource WithPopup}"  />
                        </StackPanel>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>
        </StackPanel>
    </Grid>

如果我有任何误解,您可以提供所有其他必要的代码段,以便其他人可以从头开始重现您的问题以及有关结果的详细说明,包括任何异常消息.

If I have any misunderstanding, you can include all necessary code snippets for anyone else to be able to reproduce your issue from scratch along with a detailed description about the results including any exception messages.


最好的问候,


Best Regards,

吕汉楠


这篇关于在ItemsSource中使用公共属性填充ItemsControl,而无需添加代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆