2015-12-18 179 views
2

我是新的MVVVM所以請原諒我,如果這是愚蠢的問題。我正在使用此示例http://www.codeproject.com/Articles/36848/WPF-Image-Pixel-Color-Picker-Element幷包含庫以獲取圖像的用戶像素指示的顏色。它看起來很不錯,在矩形選擇顏色dipsalys,但我neeed綁定選擇值viewmodel。MVVM綁定cutom屬性查看模型

這裏是我的XAML代碼:

<Window x:Class="MovieEditor.View.PixelSelector" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:ctrls="clr-namespace:ColorPickerControls;assembly=ColorPickerControls" 
     xmlns:local="clr-namespace:MovieEditor.MVVMCommon" 
     Title="FilterDesigner" Height="550" Width="550" 
       Icon="..\Resources\Images\icon.ico" 
     xmlns:VM="clr-namespace:MovieEditor.ViewModel"> 
     <Window.DataContext> 
     <VM:PixelSelectorVM/> 
    </Window.DataContext> 
    <Window.Resources> 
     <local:ColorToBrushConverter x:Key="ColorToBrushConverter"/> 
    </Window.Resources> 
    <Grid Background="#FF191919" > 
     <DockPanel> 
      <Grid Margin="10,10,10,1"> 
       <Grid.RowDefinitions> 
        <RowDefinition/> 
        <RowDefinition Height="Auto" MinHeight="38"/> 
       </Grid.RowDefinitions> 

       <Border BorderBrush="White" BorderThickness="5" Margin="0,39,0,11"> 
        <ctrls:ImageColorPicker Binding.XmlNamespaceManager="{Binding p PixelSelectorVM.MyImageColorPicker, UpdateSourceTrigger=PropertyChanged}" 
              x:Name="image" Source ="{Binding Frame}" Margin="0,36,0,0" 
              /> 

       </Border> 
       <Border Width="77" 
        HorizontalAlignment="Center" 
        BorderBrush="White" BorderThickness="1" Margin="263,2,182,435"> 
        <Rectangle Fill="{Binding ElementName=image, Path=SelectedColor, 
        Converter={StaticResource ColorToBrushConverter}}" RenderTransformOrigin="0.549,0.429" Margin="1"/> 

       </Border> 
       <Button Content="Save" Command="{Binding Save}" Margin="165,0,0,4" Grid.Row="1" HorizontalAlignment="Left" Width="60"/> 
       <Label Content="Selected pixel color:" HorizontalAlignment="Left" Height="18" Margin="140,11,0,0" VerticalAlignment="Top" Width="110"/> 
       <Button Content="Cancel" Command="{Binding Cancel}" Margin="0,1,165,4" HorizontalAlignment="Right" Width="60" RenderTransformOrigin="0.5,0.5" Grid.Row="1"> 
       </Button> 
      </Grid> 
     </DockPanel> 
    </Grid> 
</Window> 
</code> 

這裏是我的視圖模型:

public class PixelSelectorVM : ViewModelBase 
     { 
      private BitmapImage frame; 
      public MainWindowVM parentMainWindowVM; 
      private ImageColorPicker imageColorPicker; 

      public ImageColorPicker MyImageColorPicker 
      { 
       get 
       { 
        return this.imageColorPicker; 
       } 
       set 
       { 
        this.imageColorPicker = value; 
        OnPropertyChanged("MyImageColorPicker"); 
       } 
      } 
      public BitmapImage Frame 
      { 
       get 
       { 
        return this.frame; 
       } 
       set 
       { 
        this.frame = value; 
        OnPropertyChanged("Frame"); 
       } 
      } 

      public PixelSelectorVM(BitmapImage image, MainWindowVM mainWindowVM) 
      { 
       this.frame = image; 
       this.parentMainWindowVM = mainWindowVM; 
       this.imageColorPicker = new ImageColorPicker(); 
       this.imageColorPicker.Source = image; 
      } 

      public PixelSelectorVM() { } 

      public ICommand Save 
      { 
       get 
       { 
        return new RelayCommand(SaveExecute); 
       } 
      } 

      public ICommand Cancel 
      { 
       get 
       { 
        return new RelayCommand(CancelExecute); 
       } 
      } 

      private void SaveExecute() 
      { 

      } 

      private void CancelExecute() 
      { 

      } 
     } 

請給我建議的解決方案如何傳遞所選擇的顏色來查看模型

回答

2

你應該能夠將ImageColorPicker的SelectedColor綁定到ViewModel的屬性。

所以在XAML添加綁定:

SelectedColor="{Binding MySelectedColor, Mode=TwoWay}" 

而且在VM添加MySelectedColor屬性:

private Color selectedColor; 
    public Color MySelectedColor 
    { 
     get 
     { 
      return this.selectedColor; 
     } 
     set 
     { 
      this.selectedColor = value; 
      OnPropertyChanged("MySelectedColor"); 
     } 
    } 

當控件的SelectedColor的變化,它會自動更新您的虛擬機MySelectedColor。

+0

這並不容易,因爲selectedColor是一個只讀的依賴屬性 – dacio