Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.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
Wpf CustomControl中的绑定_Wpf_Binding_Mvvm_Custom Controls - Fatal编程技术网

Wpf CustomControl中的绑定

Wpf CustomControl中的绑定,wpf,binding,mvvm,custom-controls,Wpf,Binding,Mvvm,Custom Controls,我创建了一个自定义控件,它(除其他外)能够在ItemsControl中显示许多展开器 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System

我创建了一个自定义控件,它(除其他外)能够在ItemsControl中显示许多展开器

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:sys="clr-namespace:System;assembly=mscorlib"> <Style TargetType="{x:Type NxTabControl:NxCategoryControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type NxTabControl:NxCategoryControl}">
                   <ScrollViewer x:Name="contentScrollViewer" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
                            <StackPanel HorizontalAlignment="Stretch" DockPanel.Dock="Bottom" VerticalAlignment="Stretch" Background="Transparent">
                                <ItemsControl ItemsSource="{TemplateBinding Content}">
                                    <ItemsControl.Template>
                                        <ControlTemplate TargetType="ItemsControl">
                                            <Border>
                                                <ItemsPresenter/>
                                            </Border>
                                        </ControlTemplate>
                                    </ItemsControl.Template>
                                    <ItemsControl.ItemsPanel>
                                        <ItemsPanelTemplate>
                                            <StackPanel Orientation="Vertical" />
                                        </ItemsPanelTemplate>
                                    </ItemsControl.ItemsPanel>
                                </ItemsControl>
                            </StackPanel>
                        </ScrollViewer>
                    </DockPanel>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style> </ResourceDictionary>
也没有其他绑定指向“ElementName”。例如,我还通过名称和其他元素绑定到XamDataGrids.ActiveRecords。所有绑定都不起作用

Snoop告诉我栅格的宽度属性: System.Windows.Data错误:4:找不到引用为“ElementName=ScrollViewerAddresse”的绑定源。 BindingExpression:Path=ActualWidth;DataItem=null;目标元素为“网格”;目标属性为“宽度”(类型为“双精度”)

我在扩展器中使用的其他绑定,绑定到usercontrol的ViewModel也可以工作,例如:

Header={Binding ExpanderHeader} 

欢迎任何帮助!谢谢

您的
ObservableCollection
是否实际包含控件的实例?因为这完全违反了MVVM,而且不起作用。您需要在此集合中保存模型,其中包含将由TabControl模板表示的数据,这些模板最终将与每个模板中的展开器绑定。是的,包含展开器(不过是扩展展开器),为自定义控件的用户提供了将他们想要的任何内容放入控件的可能性。我基本上想要实现的是有一个类似于TabControl的东西,其中每个选项卡都在一个扩展器中,可以通过单击“select tab a”展开并滚动到顶部。。。“选择选项卡n”按钮。这基本上是可行的,唯一不可行的是绑定到扩展器中的元素。您跳过了我说的“这完全违反了MVVM”的部分。您的扩展器进入数据模板。您创建的模型在绑定到某个ItemsControl类型的ObservableCollection中公开(不确定什么是合适的,因为我没有研究您的问题)。你自下而上做错了。Thx不会使用ItemsControl,而是使用StackPanel,而是从代码隐藏中填充来完成这项任务。现在所有的绑定都可以工作了。如果你想使用MVVM,你应该研究一下。
<NxTabControl:NxCategoryControl x:Name="catControl" DockMode="Top" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderBrush="Green">
   <TabControl:CategoryControl.Content>
         <Expander Margin="0 3 0 3"  IsExpanded="True" Header="Adresse" >
            <ScrollViewer  x:Name="ScrollViewerAdresse" HorizontalAlignment="Stretch" ockPanel.Dock="Left">
                <Grid Width="{Binding ElementName=ScrollViewerAdresse, Path=ActualWidth, Converter={StaticResource HalfWidthConverter}}">
                </Grid>
            </ ScrollViewer>
         </Expander>
        <Expander>
        </Expander>
        <Expander Header={Binding ExpanderHeader}>
        </Expander>
   </TabControl:CategoryControl.Content>
Grid Width="{Binding ElementName=ScrollViewerAdresse, Path=ActualWidth}"
Header={Binding ExpanderHeader}