Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.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

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
C# WPF Usercontrol项未显示_C#_Wpf_Binding - Fatal编程技术网

C# WPF Usercontrol项未显示

C# WPF Usercontrol项未显示,c#,wpf,binding,C#,Wpf,Binding,我有一个在wpf中构建的条形图。用户指定栏中的项目 BarChart.xaml.cs public partial class BarChart : UserControl { List<BarItem> BarItems = new List<BarItem>(); List<Bar> Bars = new List<Bar>(); public List<BarItem> Items {

我有一个在wpf中构建的条形图。用户指定栏中的项目

BarChart.xaml.cs

public partial class BarChart : UserControl
{
    List<BarItem> BarItems = new List<BarItem>();
    List<Bar> Bars = new List<Bar>();
    public List<BarItem> Items
    {
        get { return BarItems; }
        set
        {
            BarItems = value;
            Bars.Clear();
            int i=0;
            this.LayoutRoot.Children.Clear();
            //This line should show up but doesn't suggesting that that the property is not being set?
            Debug.WriteLine("SET");
            foreach(BarItem Item in BarItems){
                Bar ThisBar=new Bar();
                ThisBar.CurrentLable=Item.Lable;
                ThisBar.CurrentValue=Item.Value;
                Debug.WriteLine("{0}:{1} at {2}",Item.Lable,Item.Value,(i*55));
                ThisBar.CurrentX=i*55;
                this.AddChild(ThisBar);
                i++;
            }
            Debug.WriteLine(i);
        }
    }
    public BarChart()
    {
        this.InitializeComponent();
    }
}
要添加新的条形图,请运行

<local:BarChart>
    <local:BarChart.Items>
        <local:BarItem Lable="B" Value="75"/>
        <local:BarItem Lable="A" Value="50"/>
    </local:BarChart.Items>
</local:BarChart>

然而,根据产出,没有添加任何项目。我做错了什么?还有更好的方法吗?

您的问题是,属性设置程序实际上从未被调用过

按照您在XAML中所做的方式添加BarItem对象,将把这些项添加到现有列表实例中。仅当您将列表设置为新实例时,才会调用属性设置器

因此,我将在代码隐藏中创建一个新列表,并在其中设置属性。这样做将调用setter,代码将运行。您可能需要为条形图命名,以便可以引用它

XAML

代码隐藏


您的问题是,实际上从未调用属性设置程序

按照您在XAML中所做的方式添加BarItem对象,将把这些项添加到现有列表实例中。仅当您将列表设置为新实例时,才会调用属性设置器

因此,我将在代码隐藏中创建一个新列表,并在其中设置属性。这样做将调用setter,代码将运行。您可能需要为条形图命名,以便可以引用它

XAML

代码隐藏


我会用装订的。背后的代码将只是持有一个财产:

public partial class BarChart : UserControl
{
    private List<BarItem> _items;
    public List<BarItem> Items
    {
        get { return _items ?? (_items = new List<BarItem>()); }
        set { _items = value; }
    }

    public BarChart()
    {
        InitializeComponent();
    }
}
而UI将完成其余工作:

<ItemsControl ItemsSource="{Binding Items, ElementName=BarChartList}">

    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <local:Bar CurrentLable={Binding Lable} CurrentValue={Binding Value}/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>

</ItemsControl>
用户界面中已更改的内容:

与网格不同,ItemsControl具有有用的ItemsSource属性,这使事情变得更简单; StackPanel与Grid不同,无需通过堆叠来手动定位项目。如果希望项目之间有间隙,可以将“局部:条形边距”设置为常量。
我会用装订的。背后的代码将只是持有一个财产:

public partial class BarChart : UserControl
{
    private List<BarItem> _items;
    public List<BarItem> Items
    {
        get { return _items ?? (_items = new List<BarItem>()); }
        set { _items = value; }
    }

    public BarChart()
    {
        InitializeComponent();
    }
}
而UI将完成其余工作:

<ItemsControl ItemsSource="{Binding Items, ElementName=BarChartList}">

    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <local:Bar CurrentLable={Binding Lable} CurrentValue={Binding Value}/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>

</ItemsControl>
用户界面中已更改的内容:

与网格不同,ItemsControl具有有用的ItemsSource属性,这使事情变得更简单; StackPanel与Grid不同,无需通过堆叠来手动定位项目。如果希望项目之间有间隙,可以将“局部:条形边距”设置为常量。
另外,我认为您可以使用ObservableCollection来实现这一点,然后注册到EventCollectionChanged以控制插入和删除

但我认为这样做的正确方法是使用ICollection的DependencyProperty,然后如果集合实现ICollectionChanged接口,您可以控制更新视图的插入和删除。如果您希望绑定到此集合,这将非常有用


希望这能对您有所帮助,…

另外,我认为您可以使用ObservableCollection来完成此操作,然后注册到EventCollectionChanged以控制插入和删除

但我认为这样做的正确方法是使用ICollection的DependencyProperty,然后如果集合实现ICollectionChanged接口,您可以控制更新视图的插入和删除。如果您希望绑定到此集合,这将非常有用


希望这能对您有所帮助,

您如何将BarItems添加到条形图中?BarChart.xaml.cs中有一个名为Items的属性。此集合属性循环遍历这些项并将它们添加到布局图。这里有@Rachel的回答,您说在调试窗口中设置了0。这意味着您将项目设置错误。你可以发布一个向条形图中添加项目的最小示例吗?我已经将其添加到底部,上面写着添加你运行的新条形图。我使用Blend构建了它,这是它给我的默认代码。这是一个糟糕做法的组合。您应该使用依赖项属性和MVVM。如何将BarItems添加到条形图中?BarChart.xaml.cs中有一个名为Items的属性。此集合属性循环遍历这些项并将它们添加到布局图中。这里有@Rachel的回答,您说调试窗口中设置了0。这意味着您将项目设置错误。你可以发布一个向条形图中添加项目的最小示例吗?我已经将其添加到底部,上面写着添加你运行的新条形图。我使用Blend构建了它,这是它给我的默认代码。这是一个糟糕做法的组合。您应该使用依赖属性和MVVM。
public partial class BarChart : UserControl
{
    private List<BarItem> _items;
    public List<BarItem> Items
    {
        get { return _items ?? (_items = new List<BarItem>()); }
        set { _items = value; }
    }

    public BarChart()
    {
        InitializeComponent();
    }
}
<ItemsControl ItemsSource="{Binding Items, ElementName=BarChartList}">

    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <local:Bar CurrentLable={Binding Lable} CurrentValue={Binding Value}/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>

</ItemsControl>