C# 以编程方式遍历xaml文档

C# 以编程方式遍历xaml文档,c#,windows-phone-8,winrt-xaml,C#,Windows Phone 8,Winrt Xaml,我有以下xaml: <phone:PivotItem Header="MESSAGES" x:Name="Messages"> ... <ListBox x:Name="MessagesStreamList"> <ListBox.ItemTemplate> <DataTemplate> <Grid x:Name="ParticipantImages">

我有以下xaml:

<phone:PivotItem Header="MESSAGES" x:Name="Messages">
    ...
    <ListBox x:Name="MessagesStreamList">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid x:Name="ParticipantImages">
                </Grid>
但这将返回一个0的计数


这又与ItemTemplate有关吗?

因为它位于DataTemplate中,您无法从列表框(或包含列表框的窗口)中按名称获取它

您可以做的是,将数据模板中的网格替换为用户控件,将网格放入该用户控件中,然后使用该用户控件中的代码访问网格

用户控件将定义为:

<UserControl x:Class="WpfApplication1.UserControl1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300"
         Loaded="UserControl_Loaded_1">
<Grid x:Name="ParticipantImages">

</Grid>
</UserControl>

然后在代码中,您可以执行以下操作:

    /// <summary>
/// Interaction logic for UserControl1.xaml
/// </summary>
public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }

    private void UserControl_Loaded_1(object sender, RoutedEventArgs e)
    {
        ParticipantImages.Visibility = System.Windows.Visibility.Hidden;
    }
}
//
///UserControl1.xaml的交互逻辑
/// 
公共部分类UserControl1:UserControl
{
公共用户控制1()
{
初始化组件();
}
私有void UserControl_加载_1(对象发送方,RoutedEventArgs e)
{
ParticipantImages.Visibility=System.Windows.Visibility.Hidden;
}
}

我意识到这是一个延迟响应,但今天我在寻找类似的东西时遇到了这个问题

我的目标是在DataTemplate列表框中修改某些项的属性。 以下是我的结构:

<ListBox ItemsSource="{Binding}" SelectionChanged="Items_SelectionChanged">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid>

在选择更改时,我想修改已更改项目的某些属性。我不认为这是最有效的方法,但这是我得到的

private void List_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        DependencyObject obj = (DependencyObject)sender;

        obj = loopUntilDesiredTypeIsFound<ListBoxItem>(obj);

        for (int i=0; i<VisualTreeHelper.GetChildrenCount(obj); i++)
        {
            ListBoxItem item = (ListBoxItem)VisualTreeHelper.GetChild(obj,i);
            if (item.IsSelected)
            {
                DependencyObject container = loopUntilDesiredTypeIsFound<Grid>(item);
//here we will have the Grid container for the display objects, such as textblocks, checkboxes, etc..
                break;
            }

        }
    }

    private DependencyObject getChild(DependencyObject obj)
    {
        if (VisualTreeHelper.GetChildrenCount(obj) > 0 )
        {
            return VisualTreeHelper.GetChild(obj, 0);
        }
        return null;
    }

    private DependencyObject loopUntilDesiredTypeIsFound<T>(DependencyObject obj)
    {
        while (obj != null)
        {
            int children = VisualTreeHelper.GetChildrenCount(obj);
            if (getChild(obj) is T)
            {
                return obj;
            }
            obj = getChild(obj);
        }
        return null;
    }
private void List\u SelectionChanged(对象发送者,selectionchangedventargs e)
{
DependencyObject obj=(DependencyObject)发送方;
obj=loopUntilDesiredTypeIsFound(obj);
对于(int i=0;i 0)
{
返回visualtreeheloper.GetChild(obj,0);
}
返回null;
}
找到私有DependencyObject循环直到找到所需类型(DependencyObject obj)
{
while(obj!=null)
{
int children=VisualTreeHelper.GetChildrenCount(obj);
if(getChild(obj)是T)
{
返回obj;
}
obj=getChild(obj);
}
返回null;
}

您能详细解释一下吗?为什么您不能直接访问该
网格
?当您遵循模型-视图-模型模式时,WPF可以很好地使用绑定。你尝试这样做的方式将迫使你做额外的工作。您能详细介绍一下您想做什么,以便我们能给您一个更好的答案吗?我想拼贴图像,以便根据绑定数据(将填充行和列的图像)以编程方式添加行和列。您是否尝试过
VisualTreeHelper
?如果不是的话,我想这就是你想要的。更多信息该死,我有很多东西要处理。。。你知道一个很好的小辅助函数来遍历(老实说有点像jquery选择器)“在用户控件中使用代码”-我可以举个例子吗:)?这取决于你什么时候尝试操作ParticipantImages。我将进行编辑,以演示您是否在加载控件时尝试这样做。啊,我对这整件事完全陌生,所以我想我需要很长时间才能理解这一点,但基本上。。我在.cs文件中运行initializecomponents,然后直接将视图模型的属性设置为我希望的ParticipantImages网格。这是因为在我的viewmodel中,我可以动态修改列等。听起来你可能可以通过绑定来完成,但我还是更新了…:)哦,很酷的人,我稍后会试着通读这篇文章。谢谢你抽出时间,布拉
private void List_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        DependencyObject obj = (DependencyObject)sender;

        obj = loopUntilDesiredTypeIsFound<ListBoxItem>(obj);

        for (int i=0; i<VisualTreeHelper.GetChildrenCount(obj); i++)
        {
            ListBoxItem item = (ListBoxItem)VisualTreeHelper.GetChild(obj,i);
            if (item.IsSelected)
            {
                DependencyObject container = loopUntilDesiredTypeIsFound<Grid>(item);
//here we will have the Grid container for the display objects, such as textblocks, checkboxes, etc..
                break;
            }

        }
    }

    private DependencyObject getChild(DependencyObject obj)
    {
        if (VisualTreeHelper.GetChildrenCount(obj) > 0 )
        {
            return VisualTreeHelper.GetChild(obj, 0);
        }
        return null;
    }

    private DependencyObject loopUntilDesiredTypeIsFound<T>(DependencyObject obj)
    {
        while (obj != null)
        {
            int children = VisualTreeHelper.GetChildrenCount(obj);
            if (getChild(obj) is T)
            {
                return obj;
            }
            obj = getChild(obj);
        }
        return null;
    }