我有一个内置wpf的条形图。用户指定栏内的项目。WPF Usercontrol Items not showing
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();
}
}
BarChart.xaml
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:CipherCracker"
mc:Ignorable="d"
x:Class="CipherCracker.BarChart"
x:Name="BarChartList"
d:DesignWidth="640" d:DesignHeight="480">
<Grid x:Name="LayoutRoot"/>
</UserControl>
BarItem.cs
public class BarItem
{
public int Value
{
get;
set;
}
public string Lable
{
get;
set;
}
public BarItem()
{
}
}
要添加新BARCHART运行
<local:BarChart>
<local:BarChart.Items>
<local:BarItem Lable="B" Value="75"/>
<local:BarItem Lable="A" Value="50"/>
</local:BarChart.Items>
</local:BarChart>
然而根据输出没有项目被添加。我做错了什么,有没有更好的方法?
你是如何将BarItems添加到BarChart的? – Joulukuusi
BarChart.xaml.cs中有一个名为Items的属性。此set属性循环了各项并将它们添加到LayoutRoot – Bonzo
@Rachel有答案,您说在调试窗口中存在“SET 0”。这意味着你正在设置'Items'错误。你可以向BarChart发布添加项目的最小样本吗? – Joulukuusi