多段线的绑定。我做错了什么?

发布于 2024-11-02 03:05:05 字数 1423 浏览 0 评论 0原文

我正在尝试我认为简单的事情:绘制点列表的线。 如果我将列表静态地放入窗口的 xaml 中,则一切正常。 如果我进行绑定,则不会显示任何内容。

窗口代码:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Polyline Stretch="Fill" Grid.Column="0" Name="polyline" Stroke="Red" DataContext="{Binding Points}">
    </Polyline>   
</Grid>
public partial class testWindow2 : Window
{
    AudioSignalModelView audioSignalModelView;

    public testWindow2()
    {
        InitializeComponent();

        audioSignalModelView = new AudioSignalModelView();

        this.DataContext = audioSignalModelView;
    }
}

public class AudioSignalModelView
{
    public AudioSignalModelView()
    {
        Point pointA = new Point {X=0,Y=0};
        Point pointB = new Point { X = 0.2, Y = 0.4 };
        Point pointC = new Point { X = 0.8, Y = 0.1 };
        Point pointD = new Point { X = 1, Y = 1 };

        Points.Add(pointA);
        Points.Add(pointB);
        Points.Add(pointC);
        Points.Add(pointD);
    }

    private AudioSignalTest audioSignalTest;

    private PointCollection _points = new PointCollection();
    public PointCollection Points
    {
        get { return _points; }
    }
}

我认为绑定是以某种方式完成的,因为如果我在 Points 属性的 getter 中放置一个断点,系统就会调用它......

我的代码中明显有什么问题?

I am trying what I thought a simple thing: drawing lines of a list of point.
If I put the list statically in the xaml of my window everything is ok.
If I do the bind, then nothing is displayed.

the window code:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Polyline Stretch="Fill" Grid.Column="0" Name="polyline" Stroke="Red" DataContext="{Binding Points}">
    </Polyline>   
</Grid>
public partial class testWindow2 : Window
{
    AudioSignalModelView audioSignalModelView;

    public testWindow2()
    {
        InitializeComponent();

        audioSignalModelView = new AudioSignalModelView();

        this.DataContext = audioSignalModelView;
    }
}

public class AudioSignalModelView
{
    public AudioSignalModelView()
    {
        Point pointA = new Point {X=0,Y=0};
        Point pointB = new Point { X = 0.2, Y = 0.4 };
        Point pointC = new Point { X = 0.8, Y = 0.1 };
        Point pointD = new Point { X = 1, Y = 1 };

        Points.Add(pointA);
        Points.Add(pointB);
        Points.Add(pointC);
        Points.Add(pointD);
    }

    private AudioSignalTest audioSignalTest;

    private PointCollection _points = new PointCollection();
    public PointCollection Points
    {
        get { return _points; }
    }
}

I think the binding is done somehow, because if I put a breakpoint in the getter of the Points property, it is called by the system...

What is obviously wrong in my code ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城泪 2024-11-09 03:05:05

您想要绑定到 Points 属性而不是 DataContext

<Polyline Stretch="Fill" Grid.Column="0"
          Name="polyline" Stroke="Red"
          Points="{Binding Points}">  <-- Here
</Polyline>   

MSDN 页面

You want to bind to the Points property not the DataContext.

<Polyline Stretch="Fill" Grid.Column="0"
          Name="polyline" Stroke="Red"
          Points="{Binding Points}">  <-- Here
</Polyline>   

MSDN page

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文