gpt4 book ai didi

c# - 如何将嵌套的 View 模型绑定(bind)到控件的属性

转载 作者:太空狗 更新时间:2023-10-29 23:42:37 24 4
gpt4 key购买 nike

我使用微软的 WPF 工具包的 Chart Control 编写了我自己的图表控件。我写了关于它的博客here .我的图表控件将图表中的 yaxes 堆叠在一起。正如您在文章中所读到的,这一切都运行良好。现在我想创建一个 View 模型来控制图表中的数据和轴。到目前为止,我能够向图表添加轴并在图表中显示它们。但是当我尝试添加 lineseries 时遇到问题,因为它有一个 DependentAxis 和一个 InDependentAxis 属性。我不知道如何为其分配正确的 xAxis 和 yAxis 控件。
下面您可以看到 LineSeriesViewModel 的一部分。它具有嵌套的 XAxisViewModel 和 YAxisViewModel 属性。

public class LineSeriesViewModel : ViewModelBase, IChartComponent
{

XAxisViewModel _xAxis;
public XAxisViewModel XAxis
{
get { return _xAxis; }
set
{
_xAxis = value;
RaisePropertyChanged(() => XAxis);
}
}

//The YAxis Property look the same
}

View 模型都有自己的数据模板。xaml 代码如下所示:

<UserControl.Resources>
<DataTemplate x:Key="xAxisTemplate" DataType="{x:Type l:YAxisViewModel}">
<chart:LinearAxis x:Name="yAxis" Orientation="Y" Location="Left" Minimum="0" Maximum="10" IsHitTestVisible="False" Width="50" />
</DataTemplate>
<DataTemplate x:Key="yAxisTemplate" DataType="{x:Type l:XAxisViewModel}">
<chart:LinearAxis x:Name="xAxis" Orientation="X" Location="Bottom" Minimum="0" Maximum="100" IsHitTestVisible="False" Height="50" />
</DataTemplate>

<DataTemplate DataType="{x:Type l:LineSeriesViewModel}">
<!--Binding doesn't work on the Dependent and IndependentAxis! -->
<!--YAxis XAxis and Series are properties of the LineSeriesViewModel -->
<l:FastLineSeries DependentAxis="{Binding Path=YAxis}"
IndependentAxis="{Binding Path=XAxis}"
ItemsSource="{Binding Path=Series}"/>
</DataTemplate>
<Style TargetType="ItemsControl">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<!--My stacked chart control -->
<l:StackedPanel x:Name="stackedPanel" Width="Auto" Height="Auto" Background="LightBlue">
</l:StackedPanel>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ClipToBounds="True">
<!-- View is an ObservableCollection of all axes and series-->
<ItemsControl x:Name="chartItems" ItemsSource="{Binding Path=View}" Focusable="False">
</ItemsControl>
</Grid>

这段代码运行良好。当我添加轴时,它们会被绘制出来。但是 lineseries 控件的 DependentAxis 和 InDependentAxis 保持为空,因此不会绘制该系列。如何将嵌套的 View 模型绑定(bind)到控件的属性?

最佳答案

它应该可以工作。您可以检查一些事项:

  • 系列绑定(bind)有效吗?如果是这样,请尝试找出区别。
  • 您确定 XAxis 和 YAxis 属性确实有值吗?尝试在 getter 中放置一个断点。如果达到,则绑定(bind)有效。您还可以在绑定(bind)上放置一个转换器 (IValueConverter)(它只返回它接收到的值)并在此处放置一个断点。
  • 在绑定(bind)上使用 PresentationTraceSources.TraceLevel=High 以获得更详细的跟踪(将显示在 VS 输出窗口中)。
  • DependentAxis/IndependentAxis 是否定义为 FastLineSeries 的依赖属性?

希望对你有帮助,

艾丽吉。

关于c# - 如何将嵌套的 View 模型绑定(bind)到控件的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1606708/

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