Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.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# 绑定自定义项控件_C#_Wpf_Xaml_Binding_Itemscontrol - Fatal编程技术网

C# 绑定自定义项控件

C# 绑定自定义项控件,c#,wpf,xaml,binding,itemscontrol,C#,Wpf,Xaml,Binding,Itemscontrol,我正在尝试创建自定义Itemscontrol类来显示一组不同的形状。 为了加快这个过程,我重用了CodeProject(WPF图表设计器-第4部分)中的源代码,其中所有实现都已完成,但形状是从XAML代码中添加的。出于我的目的,我需要(动态地)从代码隐藏中添加它们,所以我将CustomItemsControl绑定到ObservableCollection列表。现在,不要像这样呈现形状: <Window.Resources> <ResourceDictionary>

我正在尝试创建自定义Itemscontrol类来显示一组不同的形状。 为了加快这个过程,我重用了CodeProject(WPF图表设计器-第4部分)中的源代码,其中所有实现都已完成,但形状是从XAML代码中添加的。出于我的目的,我需要(动态地)从代码隐藏中添加它们,所以我将CustomItemsControl绑定到ObservableCollection列表。现在,不要像这样呈现形状:

 <Window.Resources>
    <ResourceDictionary>

        <Brush x:Key="ItemStroke">#FFD69436</Brush>

        <LinearGradientBrush x:Key="ItemBrush" StartPoint="0,0" EndPoint="0,1">
            <LinearGradientBrush.GradientStops>
                <GradientStop Color="#FAFBE9" Offset="0" />
                <GradientStop Color="Orange" Offset="1" />
            </LinearGradientBrush.GradientStops>
        </LinearGradientBrush>

        <Style x:Key="FlowChartItemStyle" TargetType="Path">
            <Setter Property="Fill" Value="{StaticResource ItemBrush}"/>
            <Setter Property="Stroke" Value="{StaticResource ItemStroke}"/>
            <Setter Property="StrokeThickness" Value="1"/>
            <Setter Property="StrokeLineJoin" Value="Round"/>
            <Setter Property="Stretch" Value="Fill"/>
            <Setter Property="IsHitTestVisible" Value="False"/>
            <Setter Property="SnapsToDevicePixels" Value="True"/>
        </Style>

        <Style x:Key="Process" TargetType="Path" BasedOn="{StaticResource FlowChartItemStyle}">
            <Setter Property="Data" Value="M 0,0 H 60 V40 H 0 Z"/>
        </Style>

        <DataTemplate x:Key="toolboxItemTemplate">
            <Grid Margin="5,5,5,5">
                <Path Style="{StaticResource Process}">
                </Path>
                <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding Title}" IsHitTestVisible="False" FontWeight="Bold"/>
            </Grid>
        </DataTemplate>

        <Style x:Key="Process_DragThumb" TargetType="Path" BasedOn="{StaticResource Process}">
            <Setter Property="IsHitTestVisible" Value="true"/>
            <Setter Property="Fill" Value="Transparent"/>
            <Setter Property="Stroke" Value="Transparent"/>
        </Style>

    </ResourceDictionary>
</Window.Resources>
<Grid>
    <ItemsControl Background="Yellow"
                            ItemsSource="{Binding ToolboxDataItems}" 
                            ItemTemplate="{StaticResource toolboxItemTemplate}"
                            SnapsToDevicePixels="True"  
                      ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    </ItemsControl>
</Grid>

我得到这样的东西:

 <Window.Resources>
    <ResourceDictionary>

        <Brush x:Key="ItemStroke">#FFD69436</Brush>

        <LinearGradientBrush x:Key="ItemBrush" StartPoint="0,0" EndPoint="0,1">
            <LinearGradientBrush.GradientStops>
                <GradientStop Color="#FAFBE9" Offset="0" />
                <GradientStop Color="Orange" Offset="1" />
            </LinearGradientBrush.GradientStops>
        </LinearGradientBrush>

        <Style x:Key="FlowChartItemStyle" TargetType="Path">
            <Setter Property="Fill" Value="{StaticResource ItemBrush}"/>
            <Setter Property="Stroke" Value="{StaticResource ItemStroke}"/>
            <Setter Property="StrokeThickness" Value="1"/>
            <Setter Property="StrokeLineJoin" Value="Round"/>
            <Setter Property="Stretch" Value="Fill"/>
            <Setter Property="IsHitTestVisible" Value="False"/>
            <Setter Property="SnapsToDevicePixels" Value="True"/>
        </Style>

        <Style x:Key="Process" TargetType="Path" BasedOn="{StaticResource FlowChartItemStyle}">
            <Setter Property="Data" Value="M 0,0 H 60 V40 H 0 Z"/>
        </Style>

        <DataTemplate x:Key="toolboxItemTemplate">
            <Grid Margin="5,5,5,5">
                <Path Style="{StaticResource Process}">
                </Path>
                <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding Title}" IsHitTestVisible="False" FontWeight="Bold"/>
            </Grid>
        </DataTemplate>

        <Style x:Key="Process_DragThumb" TargetType="Path" BasedOn="{StaticResource Process}">
            <Setter Property="IsHitTestVisible" Value="true"/>
            <Setter Property="Fill" Value="Transparent"/>
            <Setter Property="Stroke" Value="Transparent"/>
        </Style>

    </ResourceDictionary>
</Window.Resources>
<Grid>
    <ItemsControl Background="Yellow"
                            ItemsSource="{Binding ToolboxDataItems}" 
                            ItemTemplate="{StaticResource toolboxItemTemplate}"
                            SnapsToDevicePixels="True"  
                      ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    </ItemsControl>
</Grid>

谁能告诉我我做错了什么? 任何帮助都将不胜感激。提前谢谢

XAML:


#FFD69436
代码隐藏:

// Implements ItemsControl for ToolboxItems    
public class Toolbox : ItemsControl
{
    // Defines the ItemHeight and ItemWidth properties of
    // the WrapPanel used for this Toolbox
    public Size ItemSize
    {
        get { return itemSize; }
        set { itemSize = value; }
    }
    private Size itemSize = new Size(50, 50);

    // Creates or identifies the element that is used to display the given item.        
     protected override DependencyObject GetContainerForItemOverride()
    {
         return new ToolboxItem();
    }

    // Determines if the specified item is (or is eligible to be) its own container.        
    protected override bool IsItemItsOwnContainerOverride(object item)
    {
       return (item is ToolboxItem);
    }
}
// Represents a selectable item in the Toolbox/>.
public class ToolboxItem : ContentControl
{
    …
}

public class ToolboxDataItem : DependencyObject
{
    public string Title
    {
        get { return (string)GetValue(TitleProperty); }
        set { SetValue(TitleProperty, value); }
    }

    public static readonly DependencyProperty TitleProperty =
        DependencyProperty.Register( "Title", typeof( string ),
        typeof(ToolboxDataItem), new UIPropertyMetadata(""));

    public ToolboxDataItem(string title)
    {
        Title = title;
    }
}

public partial class DesignerCanvas : Canvas
{
    private ObservableCollection<ToolboxDataItem> toolboxDataItems = new ObservableCollection<ToolboxDataItem>();
    public ObservableCollection<ToolboxDataItem> ToolboxDataItems
    {
        get { return toolboxDataItems; }
    }

    public DesignerCanvas()
    {
        ToolboxDataItem toolboxDataItem = new ToolboxDataItem("123");
        ToolboxDataItems.Add(toolboxDataItem );
        toolboxDataItem = new ToolboxDataItem("456");
        ToolboxDataItems.Add(toolboxDataItem );
    }              
}   
//为ToolboxItems实现ItemsControl
公共类工具箱:ItemsControl
{
//定义的ItemHeight和ItemWidth属性
//用于此工具箱的包装
公共大小项目大小
{
获取{return itemSize;}
设置{itemSize=value;}
}
私有大小itemSize=新大小(50,50);
//创建或标识用于显示给定项的元素。
受保护的覆盖依赖对象GetContainerForItemOverride()
{
返回新的ToolboxItem();
}
//确定指定项是否是(或是否有资格成为)其自己的容器。
受保护的覆盖布尔IsItemItsOwnContainerOverride(对象项)
{
退货(项目为ToolboxItem);
}
}
//表示工具箱/>中的可选项。
公共类ToolboxItem:ContentControl
{
…
}
公共类ToolboxDataItem:DependencyObject
{
公共字符串标题
{
获取{return(string)GetValue(TitleProperty);}
set{SetValue(TitleProperty,value);}
}
公共静态只读从属属性TitleProperty=
从属财产登记簿(“标题”,类型(字符串),
typeof(ToolboxDataItem),新UIPropertyMetadata(“”);
公共ToolboxDataItem(字符串标题)
{
头衔=头衔;
}
}
公共部分类设计器Canvas:Canvas
{
私有ObservableCollection toolboxDataItems=新ObservableCollection();
公共ObservableCollection ToolboxDataItems
{
获取{return toolboxDataItems;}
}
公共设计师Canvas()
{
ToolboxDataItem ToolboxDataItem=新ToolboxDataItem(“123”);
添加(toolboxDataItem);
toolboxDataItem=新toolboxDataItem(“456”);
添加(toolboxDataItem);
}              
}   
MyDesigner:

<s:DesignerCanvas Focusable="true" x:Name="MyDesigner"
                  Background="{StaticResource WindowBackgroundBrush}" FocusVisualStyle="{x:Null}"
                  ContextMenu="{StaticResource DesignerCanvasContextMenu}"/>

因此,我开始尝试让应用程序与您共享的代码一起运行,但所有样式的顺序都不正确,因此在正确设置样式后,我遇到了如下示例:

 <Window.Resources>
    <ResourceDictionary>

        <Brush x:Key="ItemStroke">#FFD69436</Brush>

        <LinearGradientBrush x:Key="ItemBrush" StartPoint="0,0" EndPoint="0,1">
            <LinearGradientBrush.GradientStops>
                <GradientStop Color="#FAFBE9" Offset="0" />
                <GradientStop Color="Orange" Offset="1" />
            </LinearGradientBrush.GradientStops>
        </LinearGradientBrush>

        <Style x:Key="FlowChartItemStyle" TargetType="Path">
            <Setter Property="Fill" Value="{StaticResource ItemBrush}"/>
            <Setter Property="Stroke" Value="{StaticResource ItemStroke}"/>
            <Setter Property="StrokeThickness" Value="1"/>
            <Setter Property="StrokeLineJoin" Value="Round"/>
            <Setter Property="Stretch" Value="Fill"/>
            <Setter Property="IsHitTestVisible" Value="False"/>
            <Setter Property="SnapsToDevicePixels" Value="True"/>
        </Style>

        <Style x:Key="Process" TargetType="Path" BasedOn="{StaticResource FlowChartItemStyle}">
            <Setter Property="Data" Value="M 0,0 H 60 V40 H 0 Z"/>
        </Style>

        <DataTemplate x:Key="toolboxItemTemplate">
            <Grid Margin="5,5,5,5">
                <Path Style="{StaticResource Process}">
                </Path>
                <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding Title}" IsHitTestVisible="False" FontWeight="Bold"/>
            </Grid>
        </DataTemplate>

        <Style x:Key="Process_DragThumb" TargetType="Path" BasedOn="{StaticResource Process}">
            <Setter Property="IsHitTestVisible" Value="true"/>
            <Setter Property="Fill" Value="Transparent"/>
            <Setter Property="Stroke" Value="Transparent"/>
        </Style>

    </ResourceDictionary>
</Window.Resources>
<Grid>
    <ItemsControl Background="Yellow"
                            ItemsSource="{Binding ToolboxDataItems}" 
                            ItemTemplate="{StaticResource toolboxItemTemplate}"
                            SnapsToDevicePixels="True"  
                      ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    </ItemsControl>
</Grid>

#FFD69436
因此,如果您将此示例与您的示例联系起来,那么您的问题可能出现在以下两个地方之一:

1) TextBlock的文本绑定(请注意{前面的空格):

2) 或者工具箱的ItemsSource绑定中的路径

<s:Toolbox x:Key="FlowChartStencils" ItemsSource="{Binding ElementName=MyDesigner,    Path=ToolboxDataItems}" ItemTemplate="{StaticResource toolboxItemTemplate}" ItemSize="190,150" SnapsToDevicePixels="True"   ScrollViewer.HorizontalScrollBarVisibility="Disabled">


这里尝试类似ItemsSource=“{Binding ToolboxDataItems,ElementName=MyDesigner}”

我不确定,但您需要
s:ToolBox
中的
DisplayMemberPath
。正如我所见,DisplayMemberPath实际上是单个属性的模板,因此与DisplayMemberPath或DataTemplate的行为相同(我目前正在使用的)。我不确定,我的想法是否与您的想法相同,可能需要更多的信息,谢谢。让我给您举个例子。假设我有一个Circle类,那么我将用您的
s:ToolBox
作为其项源绑定该Circle类。我想显示圆的形状。但是我的Circle类包含半径、形状等属性。因此这里我的DisplayMemberPath将是Shape。在输出窗口中是否有任何绑定错误?我已检查了输出窗口中的错误,但没有任何错误。目前我认为问题在于为我的自定义ItemsControl类(s:ToolBox)创建自定义模板.由于某些原因,s:ToolBox未使用模板,因为形状以相同的方式表示(如图2所示)不管我是否使用模板。我已经尝试了您的两个示例,但仍然没有任何进展。无论如何,正如我在您的示例中看到的,您从我的代码中创建了什么,在应用程序内部,您使用的是ItemsControl类而不是Toolbox类(自定义ItemsControl)我想使用的。也许这就是为什么在您的示例中正确绘制形状的原因。我感谢您的努力。您的控件自定义模板可能会弄乱对象的数据绑定。数据绑定工作正常,因为我在ObservableCollection中有许多对象,相同数量的对象出现在自定义项中控件控件(s:Toolbox)。只有对象未正确显示(如图2所示)。正如您所提到的,问题可能是自定义模板。如果我对ItemsControl使用自定义模板,则显示的对象数可以,但如果我对自定义ItemsControl(s:Toolbox)使用相同的自定义模板,则显示的对象数可以形状绘制不正确。自定义项控件(s:Toolbox)似乎未使用自定义模板。