Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF如何从DataTemplate访问控制_C#_.net_Wpf - Fatal编程技术网

C# WPF如何从DataTemplate访问控制

C# WPF如何从DataTemplate访问控制,c#,.net,wpf,C#,.net,Wpf,我有一个datatemplate,其中包含一个网格,在网格中我有一个组合框 <DataTemplate x:Key="ShowAsExpanded"> <Grid> <ComboBox Name ="myCombo" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="5"

我有一个datatemplate,其中包含一个网格,在网格中我有一个组合框

<DataTemplate x:Key="ShowAsExpanded">
        <Grid>                
            <ComboBox Name ="myCombo" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="5"
                      IsSynchronizedWithCurrentItem="True"
                      ItemsSource="{Binding}"
                      ItemTemplate="{StaticResource MyItems}">
                <ComboBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <VirtualizingStackPanel />
                    </ItemsPanelTemplate>
                </ComboBox.ItemsPanel>
            </ComboBox>

        </Grid>
    </DataTemplate>

然后我有一个网格,通过样式引用该模板

<Grid>
    <ContentPresenter Name="_contentPresenter" Style="{DynamicResource StyleWithCollapse}" Content="{Binding}" />
</Grid>


如何通过myCombo后面的代码访问以基本设置其DataContext?

您需要使用FindName。首先,我甚至找不到资源(ShowAsExpanded)和ContentPresenter中的用法之间的关系。但目前,让我们假设DynamicSource应该指向ShowAsExpanded


您不能也不应该通过代码访问组合框。您应该将datacontext绑定到使用样式的网格。如果您不想这样做,您必须在运行时查找内容并搜索子组合框。

我知道的三种方法

1.使用FindName

ComboBox myCombo =
    _contentPresenter.ContentTemplate.FindName("myCombo",
                                               _contentPresenter) as ComboBox;
2.将加载的事件添加到组合框中并从那里访问它

<ComboBox Name ="myCombo" Loaded="myCombo_Loaded" ...

private void myCombo_Loaded(object sender, RoutedEventArgs e)
{
    ComboBox myCombo = sender as ComboBox; 
    // Do things..
}

有趣。我是wpf的新手。在网格上设置datacontext,它如何传播到模板中的combobox?另外,如果我的模板中有2个combobox,那么如果每个combo都有不同的datacontext,那么如何绑定每个combo的datacontext呢?datacontext将传播到child,只要不显式设置该子级的datacontext。因此,如果您在网格上设置了datacontext,ContentPresenter(以及下面的所有控件)将共享该datacontext并可以绑定到它。如果我的模板中有两个combobox,需要通过两个不同的集合进行绑定,该怎么办?创建一个包含两个集合的数据对象,然后将datacontext设置为该对象的实例。然后,您可以将这两个组合框绑定到正确的集合。您的回答是有意义的,并且将
ContentPresenter
绑定到不同的集合效果很好。但是,如果我的数据模板包含一个
列表视图
,我有时想为其添加
组描述
,该怎么办?AFAIK这只能在代码隐藏中完成。谢谢,非常有用,但Geert van Horrik的回答让我认为通过代码隐藏访问不是正确的方式。。。
private void SomeMethod()
{
    ComboBox myCombo = GetVisualChild<ComboBox>(_contentPresenter);
}
private T GetVisualChild<T>(DependencyObject parent) where T : Visual
{
    T child = default(T);
    int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
    for (int i = 0; i < numVisuals; i++)
    {
        Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
        child = v as T;
        if (child == null)
        {
            child = GetVisualChild<T>(v);
        }
        if (child != null)
        {
            break;
        }
    }
    return child;
}