System.Windows.Data错误:40:BindingExpression路径错误:在“对象”“ String”(HashCode = -842352752)上找不到“ LabelWidth”属性。 BindingExpression:Path = LabelWidth; DataItem ='String'(HashCode = -842352752);目标元素是'TextBlock'(Name ='');目标属性为“宽度”(类型为“双精度”) 我收到此错误。我不知道为什么会有这个错误。通常,由于错误的DataContext会发生此错误。
public class SignalBaseViewModel : INotifyPropertyChanged
{
public ulong Counter { get; set; }
public double MinimumLabelWidth { get; set; }
public double MinimumMargin { get; set; }
public ulong NumberOfBoxes { get; set; }
private double labelWidth;
public double LabelWidth
{
get { return labelWidth; }
set
{
labelWidth = value;
OnPropertyChanged("LabelWidth");
}
}
private double myMargin;
public double MyMargin
{
get { return myMargin; }
set
{
myMargin = value;
OnPropertyChanged("MyMargin");
}
}
public void AddLabels(double lbs)
{
TimeAxis.Clear();
Counter = 0;
MinimumMargin = 3;
MinimumLabelWidth = 27;
for (double i = 0; i < (ulong)lbs; i++)
{
TimeAxis.Add(Counter.ToString());
var tp = i;
var chachu = (TimeAxis[TimeAxis.Count-1].Length);
var boxes = (chachu / 4.0);
NumberOfBoxes = (ulong)Math.Ceiling(boxes);
Counter += (SampleStepSize * NumberOfBoxes) ;
if (NumberOfBoxes > 1)
{
i += (NumberOfBoxes - 1);
}
LabelWidth = (NumberOfBoxes * MinimumLabelWidth);
MyMargin = (MinimumMargin * NumberOfBoxes);
}
if (CanvasWidth > Convert.ToUInt64(TimeAxis.LastOrDefault()))
{
TimeAxis.Add(Counter.ToString());
}
}
}
<ItemsControl ItemsSource="{Binding TimeAxis}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Name="horizontalLabels" Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}"
Width="{Binding LabelWidth}" Background="Red" Height="30" FontSize="12"
Margin="{Binding MyMargin, Converter={StaticResource ResourceKey=marginConverter},UpdateSourceTrigger=PropertyChange}">
</TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return new Thickness(0, 0, System.Convert.ToDouble(value), 0);
}