gpt4 book ai didi

WPF MVVM 用户控件绑定(bind)问题

转载 作者:行者123 更新时间:2023-12-03 10:15:30 24 4
gpt4 key购买 nike

我有一个使用 MVVM 的应用程序。我在主窗口上有几个项目绑定(bind)到该窗口的 ViewModel。当我运行它时,一切正常。但是,当我将用户控件添加到主窗口并尝试绑定(bind)到其依赖对象之一时,它会引发异常(“对象引用未设置为对象的实例”)。异常窗口只是在屏幕上弹出,并没有链接到代码中的任何特定位置。异常中的任何其他信息都没有帮助。

我已尽力追查此事,但我没有任何运气。在 window 的构造函数中,我检查并验证了它尝试绑定(bind)的项目是否存在并且是一个对象 (int[])。我还在构造函数中手动设置了有问题的属性。
如果有人能注意到什么,这里有一些代码片段。

这是我使用用户控件并尝试绑定(bind)到“ View ”属性的地方

<local:Histogram Grid.Row="2" Grid.ColumnSpan="2"
View="{Binding Path=HistogramData}"
Foreground="{DynamicResource FontColor}"
BucketStroke="{DynamicResource BucketStrokeBrush}"
BucketFill="{DynamicResource BucketFillBrush}"
SelectedBrush="{DynamicResource FamilyEditListViewSelectedBrush}"
DisabledForegroundBrush="{DynamicResource DisabledForegroundBrush}"
AxisBrush="{DynamicResource AxisBrush}"
MaxHeight="130" />

这是我试图绑定(bind)到的 View 模型中的字段:
public int[] HistogramData
{
get
{
return histogramData;
}
set
{
if (value != histogramData)
{
histogramData = value;
RaisePropertyChanged("HistogramData");
}
}
}

在 View 模型的构造函数中,我实例化了对象
histogramData = new int[256];

最后是用户控件中的 View 属性
public static readonly DependencyProperty ViewProperty =
DependencyProperty.Register("View",
typeof(int[]),
typeof(Histogram),
new FrameworkPropertyMetadata(null,
FrameworkPropertyMetadataOptions.AffectsRender,
new PropertyChangedCallback(ViewProperty_Changed)));

public int[] View
{
get { return (int[])GetValue(ViewProperty); }
set { SetValue(ViewProperty, value); }
}

我不知道这是否足以解决任何问题,所以如果需要更多代码,请告诉我。如果有人愿意看这个,我也可以压缩这个项目。提前致谢。

最佳答案

当您在依赖属性上初始化 FrameworkPropertyMetaData 时,您可以尝试初始化数组。

 new FrameworkPropertyMetadata(new int [256],
FrameworkPropertyMetadataOptions.AffectsRender,
new PropertyChangedCallback(ViewProperty_Changed))

我认为该程序在设法将依赖项属性绑定(bind)到 viewmodel 属性之前可能会遇到空引用异常。

好的,我已经查看了您的示例项目,并认为我有一个解决方案。

更改 int[]在 View 模型中到 List<int> .
我不确定为什么会这样。我希望没有技术原因 list<int>不适合你。

这是我在解决方案中所做的更改

在 View 模型中
public List<int> CustomData
{
get
{
return new List<int>(){0,1,2,3};
}
set
{
}
}

在 arraycontrol 代码隐藏中
public static readonly DependencyProperty DataProperty =
DependencyProperty.Register("Data",
typeof(List<int>),
typeof(ArrayControl),
new FrameworkPropertyMetadata(new List<int>()));

public List<int> Data
{
get { return (List<int>)GetValue(DataProperty); }
set { SetValue(DataProperty, value); }
}

在 arraycontrol.xaml 中。刚刚添加了列表框以显示数据绑定(bind)工作
<UserControl x:Class="UserControlWithArray.Controls.ArrayControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300">
<Grid>
<TextBlock x:Name="MessageTextBlock" Text="ArrayControl"/>
<ListBox ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, Path=Data}"/>
</Grid>
</UserControl>

关于WPF MVVM 用户控件绑定(bind)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1717279/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com