2016-10-10 5 views
0

まず、私のアプリケーションでは、拡張WPFツールキットColorPickerを使用しています。カラーピッカーへのWPFバインドコマンド

私はキャンバス上で、デシリアライズされたXMLファイルに基づいていくつかの長方形を描画します(これはうまくいきます)。描画された各矩形に対して、私は、アプリケーションのユーザーが矩形の色を変更できるようにColorPickerを追加します。私はICommandインターフェイスを実装しようとしましたが、colorpickerはカスタムコマンドをバインドするのをサポートしていないようですので、私は固執しています。

internal class BackgroundInput 
{ 
    public BackgroundInput() 
    { 
    } 

    public string Color { get; set; } 
    public string Title { get; set; } 
} 

一部2

<ItemsControl Name="inputs2"> 
     <ItemsControl.ItemTemplate> 
     <DataTemplate> 
     <Grid Name="testgrid" Margin="0,0,0,5"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="*" /> 
      <ColumnDefinition Width="*" /> 
      </Grid.ColumnDefinitions> 

      <TextBlock HorizontalAlignment="Left" Grid.Column="0" Text="{Binding Title}" /> 
      <xctk:ColorPicker HorizontalAlignment="Left" 
          Grid.Column="1" 
          Name="ClrPcker_Background" 
          SelectedColor="{Binding Color}" 

          <!--tryig to bind here???--> 
      /> 
     </Grid> 
     </DataTemplate> 
     </ItemsControl.ItemTemplate> 
</ItemsControl> 

私のC#:ColorPickerの色が変更されますたび

public override void Draw(Canvas label) 
{ 
    Rectangle rect = new Rectangle(); 
    //...... drawing method 
    label.Children.Add(rect); 
} 

internal override void AddInputs(ItemsControl inputPanel) 
{ 
    inputPanel.Items.Add(new BackgroundInput() { Title = "Backgroundcolor:", Color = BackgroundColor });   
} 
//both methods get called 

だから、私は、接続された矩形がそれを更新したいが背景だ。ここに私の現在のコードです色。 ColorPickerはデータを送信するためにCommand="..."CommandParameter="..."をサポートしていないようですので、これを行う方法についてアドバイスを受けたいと思います。ありがとう!

編集

私が今持っているものの小さな例。ユーザーがカラーピッカーの色を変更すると、色を変更しようとしています。

enter image description here

EDIT

は、私は一種の作業それを得たが、私はこれが正しい方法だとは思いません。

internal class BackgroundInput 
{ 
    private string _bg; 
    public BackgroundInput() 
    { 
    } 

    public Box Box { get; internal set; } 

    public string Color 
    { 
     get { return _bg; } 
     set { _bg = value; Box.BackgroundColor = _bg; Box.redraw(); } 
    } 

    public string Title { get; set; } 
} 

長方形コード:ここに私の更新されたコードです

public class Box : Field 
{ 
     Canvas label; 
    Border _border; 

    public override void Draw(Canvas label) 
    { 
     _border = new Border(); 
     _label = label; 
     Rectangle x= new Rectangle(); 

     label.Children.Add(x); 
    } 

    internal override void AddInputs(ItemsControl inputPanel) 
    { 
     inputPanel.Items.Add(new BackgroundInput() { Title = "Background:", Box = this, Color = BackgroundColor });   
    } 

    public void redraw() 
    { 
     _label.Children.Remove(_label); 
     Draw(_label); 
    } 
} 
+0

対応するBackgroundInput.Colorに背景をバインドします。いくつかのコマンドを実行する必要はありません。あなたのコメントのために – Mat

+0

@Mat thansk!私はWPFの初心者です(2週間前に始まった:p)これはどうやってバインドしますか? – Markinson

+0

キャンバスをレイアウトパネルとして使用してもよろしいですか? – Mat

答えて

1

は、私は非常にMVVMパターンを使用することをお勧めいたします。レイアウトパネル(その場合はキャンバス)に描画したいオブジェクトのリストがあると思います。

最初に必要なのはViewModelです。

public class MyRectangleViewModel : INotifyPropertyChanged 
{ 
    private Color _background; 
    public Color Background 
    { 
     get 
     { 
      return _background; 
     } 
     set 
     { 
      _background = value; 
      OnPropertyChanged(); 
     } 
    } 

    /// <summary> 
    /// Occurs when [property changed]. 
    /// </summary> 
    public event PropertyChangedEventHandler PropertyChanged; 

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

INotifiyPropertyChangedに注意してください。これにより、 "WPF binding magic"が追加されます。プロパティが変更されるたびに、PropertyChangedイベントが発生します。 SOとWebのチュートリアルや例がたくさんあります。

ItemsControlなどを使用して、すべてのアイテムをItemsSourceにバインドできます。

とにかく。何らかの理由でコード内にすべての要素を作成したいようです。だからここにコードの解決策があります。#ItGivesMeTheCreeps:

/// <summary> 
/// All my recangles 
/// </summary> 
private ObservableCollection<MyRectangleViewModel> AllMyRecangles = new ObservableCollection<MyRectangleViewModel>(); 
/// <summary> 
/// Call this method if you add/remove objects to your list. 
/// </summary> 
public void RefreshObjects() 
{ 
    this.myCanvas.Children.Clear(); 

    foreach (MyRectangleViewModel item in AllMyRecangles) 
    { 
     Rectangle newRectangle = new Rectangle(); 

     //set the DataContext 
     newRectangle.DataContext = item; 

     //create the binding 
     Binding b = new Binding(); 
     b.Source = item; 
     b.Path =new PropertyPath(nameof(Background)); 
     b.Mode = BindingMode.TwoWay; 
     b.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; 

     //setup the binding XAML: <Rectangle Fill = {Binding Path="Background", Mode=TwoWay, UpdateSourceTrigger ="PropertyChanged" /> 
     BindingOperations.SetBinding(newRectangle, Rectangle.FillProperty, b); 


     //add the rectangle to the canvas 
     myCanvas.Children.Add(newRectangle); 
    } 
} 
+0

あなたは爆弾ですか? ! :Dありがとう! XMLファイルからすべてのデータを含む1つの大きなオブジェクトにオブジェクトを逆シリアル化しているので、私がコードですべてそれをやっている理由です。次に、そのオブジェクトにある各アイテムをforeachを使って描画します。 – Markinson

+0

あなたは1つの大きなオブジェクトにもバインドできます;-) WPFはすべてバインドについてです – Mat

+0

ところで、私の投稿の編集を見ましたか?それについてあなたの意見は何ですか? – Markinson

関連する問題