2016-03-14 61 views
0

我有一個矩形,其背景顏色綁定到一個變量。當我點擊矩形時,我想調出一個顏色選擇器,以便用戶可以改變矩形的顏色。編輯一個CommandParameters綁定變量

我已經是當我改變o它不會改變WorkflowModel.BackgroundColour如我期望的問題,它只會改變o

任何想法,我怎麼能做到這一點,或者如果有一些方法可以讓我通過一個refWorkflowModel.BackgroundColour並編輯它。

XAML:

<Rectangle Stroke="Black" StrokeThickness="1" Grid.Row="6" Grid.Column="1" Height="25" Width="25"> 
    <Rectangle.InputBindings> 
     <MouseBinding MouseAction="LeftClick" 
         CommandParameter="{Binding WorkflowModel.BackgroundColour}"          
         Command="{Binding ChangeColorCommand}"/> 
    </Rectangle.InputBindings> 
    <Rectangle.Fill> 
     <SolidColorBrush Color="{Binding WorkflowModel.BackgroundColour}"/> 
    </Rectangle.Fill> 
</Rectangle> 

命令:

private void ChangeColorCommandExecute(object o) 
{ 
    Views.DialogViews.ColorPickerDialog cpd = new Views.DialogViews.ColorPickerDialog((Color)o); 
    cpd.ShowDialog(); 

    if(cpd.DialogResult == Views.DialogViews.ColorPickerDialog.DialogResults.Ok) 
    { 
     o = cpd.SelectedColor; 
    } 
} 

public ICommand ChangeColorCommand 
{ 
    get { return new RelayCommand(o => ChangeColorCommandExecute(o)); } 
} 

回答

1

更改您的CommandParameter綁定到WorkflowModel。然後在ChangeColorCommandExecute中將object o投射到WorkflowModel並使用BackgroundColour屬性。

如果它不適合讓你的命令處理程序採取對WorkflowModel依賴包裹你的顏色在自己ColourViewModel並綁定命令參數爲ColourViewModel

public class ColourViewModel :INotifyPropertyChanged 
{ 
    private Color _colour; 
    public Color Colour 
    { 
    get { return _colour; } 
    set 
    { 
     _colour = value; 
     // raise change notification 
    } 
    } 
} 

private void ChangeColorCommandExecute(object o) 
{ 
    ColourViewModel cvm = o as ColourViewModel; 
    if (cvm != null) 
    { 
     Views.DialogViews.ColorPickerDialog cpd = new Views.DialogViews.ColorPickerDialog(cvm.Colour); 
     cpd.ShowDialog(); 

     if(cpd.DialogResult == Views.DialogViews.ColorPickerDialog.DialogResults.Ok) 
     { 
     cvm.Colour = cpd.SelectedColor; 
     } 
    } 
}