2013-10-11 139 views
0

我有一個包含2 DoubleUpDown用戶控制,我已綁定點控制,當我從外面變化值WPF綁定點到用戶控件

<DoubleUpDown x:Name="X" Grid.Column="1" Grid.Row="0" Value="{Binding Path=Value.X, Mode=TwoWay" /> 
<DoubleUpDown x:Name="Y" Grid.Column="1" Grid.Row="1" Value="{Binding Path=Value.Y, Mode=TwoWay}" /> 

控制得到更新不錯,但是當我改變值保持不變控制數據。

我限值於用戶控制從碼內

Point2DEditorView editor = new Point2DEditorView(); 
Binding binding = new Binding("Value"); 
binding.Mode = BindingMode.TwoWay; 
editor.SetBinding(Point2DEditorView.ValueProperty, binding); 

Point2DEditorView.Value和當我插入新座標轉換控制也改變。但是這並不影響綁定的價值。

回答

0

Point是一個值類型數據。正因爲如此,當你綁定它來控制裝箱和拆箱時發生。欲瞭解更多信息,請參閱this。所以,你可以很容易解決通過創建自己的類此問題(而不是struct!):

class MyPoint 
{ 
    public int X { set; get; } 
    public int Y { set; get; } 
} 

,然後綁定這個對象到你的控制,你會看到所有的作品,你期望的那樣。

更新 首先你的DoubleUpDown is'n在標準整箱,我認爲你的問題在它。有一個簡單的例子,其中一切都按預期工作。我創建了它的簡單增減的控制:

Point類

public class Point2D : INotifyPropertyChanged 
{ 
    private double x; 
    private double y; 

    public double X 
    { 
     set 
     { 
      if (value.Equals(x)) return; 
      x = value; 
      OnPropertyChanged(); 
     } 
     get { return x; } 
    } 

    public double Y 
    { 
     set 
     { 
      if (value.Equals(y)) return; 
      y = value; 
      OnPropertyChanged(); 
     } 
     get { return y; } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 


    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) 
    { 
     var handler = PropertyChanged; 
     if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 

增減的XAML

<UserControl x:Name="doubleUpDown" x:Class="PointBind.DoubleUpDown" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     mc:Ignorable="d" d:DesignWidth="105" Height="33"> 
<StackPanel Orientation="Horizontal" DataContext="{Binding ElementName=doubleUpDown}"> 
    <TextBox Margin="5,5,0,5" Width="50" Text="{Binding Value}" /> 
    <Button x:Name="Up" x:FieldModifier="private" Margin="5,5,0,5" Content="˄" Width="20" Click="Up_Click" /> 
    <Button x:Name="Down" x:FieldModifier="private" Margin="0,5,0,5" Content="˅" Width="20" Click="Down_Click" /> 
</StackPanel> 
</UserControl> 

增減的的.cs

public partial class DoubleUpDown : UserControl 
{ 

    public double Value 
    { 
     get { return (double)GetValue(ValueProperty); } 
     set { SetValue(ValueProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for Value. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty ValueProperty = 
     DependencyProperty.Register("Value", typeof(double), typeof(DoubleUpDown), new PropertyMetadata(0.0)); 



    public DoubleUpDown() 
    { 
     InitializeComponent(); 
     DataContext = this; 
    } 

    private void Up_Click(object sender, RoutedEventArgs e) 
    { 
     Value++; 
    } 

    private void Down_Click(object sender, RoutedEventArgs e) 
    { 
     Value--; 
    } 


} 

Point2DEditorView XAML

<UserControl x:Name="point2DEditorView" x:Class="PointBind.Point2DEditorView" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     mc:Ignorable="d" 
     xmlns:local="clr-namespace:PointBind" 
     d:DesignHeight="300" d:DesignWidth="300"> 
<StackPanel> 
    <local:DoubleUpDown Value="{Binding Point.X, ElementName=point2DEditorView, Mode=TwoWay}"/> 
    <local:DoubleUpDown Value="{Binding Point.Y, ElementName=point2DEditorView, Mode=TwoWay}"/> 
</StackPanel> 
</UserControl> 

增減的的.cs

public partial class Point2DEditorView : UserControl 
{ 

    public Point2D Point 
    { 
     get { return (Point2D)GetValue(PointProperty); } 
     set { SetValue(PointProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for Point. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty PointProperty = 
     DependencyProperty.Register("Point", typeof (Point2D), typeof (Point2DEditorView), 
      new PropertyMetadata(new Point2D {X = 10, Y = 20})); 


    public Point2DEditorView() 
    { 
     InitializeComponent(); 
    } 

} 

測試形式XAML

<Window x:Class="PointBind.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:PointBind" 
    Title="MainWindow" Height="350" Width="525"> 
<Grid> 
    <local:Point2DEditorView x:Name="pointEditor"/> 
    <Button Content="Button" HorizontalAlignment="Left" Margin="39,121,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/> 

</Grid> 
</Window> 

和測試形式的.cs

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    private void Button_Click(object sender, RoutedEventArgs e) 
    { 
     pointEditor.Point = new Point2D{X = 300, Y = 400}; 
    } 
} 

希望這會有所幫助。

+0

感謝您的快速響應。 Point2D已經上課了。我能做的唯一事情就是在每個數值控制值更改的事件上創建新的Value值= new Point2D(xcontrol.Value,ycontorl.Value);它有幫助。 – Artyom

+1

你使用什麼庫?我在標準庫中找不到Point2D。 – Deffiss

+0

這是我自己的班級。代表一個觀點(不要問爲什麼我應該寫另一個觀點類:()。 – Artyom