3

私のアプリケーションには、この単純なDataGridがあります。ソースのどこかで、そのItemsSourceプロパティをにバインドしますObservableCollection<System.Windows.Points>。したがって、ポイントはに表示されますDataGrid。問題は、TwoWayバインディングを設定したことですが、のポイント座標値を変更してもDataGrid、実際のポイント値ObservableCollectionは変更されません。

何が問題になっていますか?

<DataGrid Name="pointList" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="X" Width="200">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding Path=X, Mode=TwoWay}"></TextBox>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn Header="Y" Width="200">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding Path=Y, Mode=TwoWay}"></TextBox>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

私はこれを見たことがありますが、私の問題は異なります。

4

1 に答える 1

1

System.Windows.Points構造体です。そのプロパティを正しくバインドすることはできません。

なんで?これを行うと、のプロパティが現在ののプロパティにText="{Binding X, Mode=TwoWay}"バインドされるためです。TextTextBoxXDataContext

DataContextこれは...構造体 System.Windows.PointsでありPoint、データバインディングが変更するのは、割り当てたものではありませんDataContext

あなたの問題を解決するために。クラスPointを使用して独自のタイプを作成します。

public class Point : INotifyPropertyChanged
{
    private double x;
    public double X
    {
        get { return x; }
        set
        {
            if (x != value)
            {
                x = value;
                OnPropertyChanged("X");
            }
        }
    }
    private double y;
    public double Y
    {
        get { return y; }
        set
        {
            if (y != value)
            {
                y = value;
                OnPropertyChanged("Y");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

バインディングに使用UpdateSourceTrigger=LostFocusします。

<TextBox Text="{Binding Path=Y, Mode=TwoWay, UpdateSourceTrigger=LostFocus}"></TextBox>
于 2013-01-30T15:27:51.853 に答える