2013-01-20 36 views
1

應用程序ia使用Microsoft使用的Microsoft Lync客戶端用於此目的。在上下文我正在接觸(其爲具有類似的名稱,圖像,可用性等特性LyncClient的對象)在列表視圖,並且其中定義如下數據模板加載它們中的一種:使用WPF如何使用綁定到灰度圖像在某些情況下

<DataTemplate x:Key="ContactsTemplate"> 
     <Grid HorizontalAlignment="Left" Width="150" Height="150" Margin="10"> 
      <Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}"> 
       <Image Source="{Binding Image}" Stretch="UniformToFill" AutomationProperties.Name="{Binding Title}"/> 
      </Border> 
      <StackPanel VerticalAlignment="Bottom" Background="{Binding Availability, Converter={StaticResource AvailabilityToPresenceColor}}" Opacity="0.75"> 
       <TextBlock Text="{Binding Name}" Foreground="{StaticResource ListViewItemOverlayForegroundThemeBrush}" Style="{StaticResource TitleTextStyle}" Height="20" Margin="15,0,15,15"/> 
      </StackPanel> 
     </Grid> 
    </DataTemplate> 

它有一個網格容器,其中我們有一個圖像和文本塊控件,顯示聯繫人的圖像和名稱,如下圖所示,堆疊面板的背景綁定到lync的可用性屬性聯繫人帶轉換器的對象,可映射可用性狀態改爲顏色,以便例如當聯繫人可用性繁忙時,堆疊面板的背景將變爲紅色。

我想對圖像控制也有類似的效果。

我是新來綁定這麼totaly失去了這個bindig概念。

我的想法是:有對圖像的影響evend處理程序,所以我想利用這個宗旨,運用

和一些條件我想使用一些代碼,我需要下轉換器內獲取圖像源,但是因爲我們通過綁定獲取圖像源,請給我建議您的想法。


好如u可以在代碼 <Image Source="{Binding Image}" Stretch="UniformToFill" AutomationProperties.Name="{Binding Title} effect="{Binding Availability, Converter={StaticResource AvailabilityToPresenceColor}}"/> 我只是結合圖像控制源與聯繫對象一個性質看。我想將Contact對象的Availability屬性發送給IValueConverter的Convert方法,或者我想將整個圖像與整個圖像進行綁定。Contact對象如果可能...或者其他方式,請讓我知道。

##################### 評論附件
var bitmap = new BitmapImage(); 
    bitmap.BeginInit(); 
    MemoreyStream ms=new MemoryStream(_image); 
    bitmap.StreamSource = stream; 
    bitmap.CacheOption = BitmapCacheOption.OnLoad; 
    bitmap.EndInit(); 
var grayBitmapSource = new FormatConvertedBitmap(); 
    grayBitmapSource.BeginInit(); 
    grayBitmapSource.Source = ms; 
    grayBitmapSource.DestinationFormat = PixelFormats.Gray32Float; 
    grayBitmapSource.EndInit(); 
..... 

現在的事情是我有grayBitmapSource它的類型是FormatConvertedBitmap和我的不知道如何將其再次轉換爲Stream。

+0

你可能想看看在WPF着色效果庫,其中包含一個單色效果你可以申請使用一些觸發http://wpffx.codeplex.com/ –

+0

@HighCore well thanx爲您的答覆,但問題的意圖不是要應用顏色效果或圖像處理...我的問題是更相對於綁定,我有一個綁定的問題。 請再次通過我的問題,你將能夠找出我要求 –

回答

0

我建議在看這篇文章的圖像處理在WPF:http://www.codeproject.com/Articles/237226/Image-Processing-is-done-using-WPF

使用圖像處理邏輯,您爲每個可用性狀態不同的圖片。您可以使用IValueConverter,但這意味着每次可用性狀態更改時都必須重新處理映像。相反,你可以簡單地改變你的聯繫類,所以,當你改變可用性特性,它會自動發出信號WPF獲得Image屬性所引用的圖片:

public class Contact : INotifyPropertyChanged 
{ 
    // EDIT: INotifyPropertyChanged implementation. 
    public event PropertyChangedEventHandler PropertyChanged; 

    public void NotifyPropertyChanged(String propertyName) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
    // EDIT: INotifyPropertyChanged implementation. 

    private ContactAvailability _Availability; 

    public ContactAvailability Availability 
    { 
     get { return _Availability; } 
     set 
     { 
      _Availability = value; 
      NotifyPropertyChanged("Availability"); 
      NotifyPropertyChanged("Image"); 
     } 
    } 

    public BitmapImage _AvailablePicture; 
    public BitmapImage _BusyPicture; 

    public BitmapImage Image 
    { 
     get 
     { 
      switch (this.Availability) 
      { 
       case ContactAvailability.Available: 
        return this._AvailablePicture; 
       case ContactAvailability.Busy: 
        return this._BusyPicture; 
       default: 
        throw new NotImplementedException(); 
      } 
     } 
    } 
} 

編輯(太長的評論):

我添加了代碼來實現INotifyPropertyChanged接口。這在WPF中很常見,所以我認爲你已經熟悉了這種方法。

在你的例子中,Image.SourceDepencyProperty。當一個類實現INotifyPropertyChanged時,你可以告訴WPF它的一個屬性已經改變。您只需使用更改的屬性名稱來提升NotifyPropertyChanged事件。這表示WPF更新綁定到給定屬性的所有DepencyProperty

這與圖像處理方面的綁定有何不同。我的意思是每當可用性發生變化時,以這種方式也應該執行圖像處理代碼。我對不對@bouvierr?

否。在這種情況下,我們只會執行固定次數的圖像處理來創建每個可用性狀態的圖片(在我的示例中爲每個聯繫人2次)。例如,我們可以在應用程序啓動過程中創建所有圖片(3次聯繫x 2狀態= 6張圖片),並將它們存儲在每個聯繫人的_AvailablePicture_BusyPicture字段中。

這裏是重要的一部分:當我們設置Availability屬性,我們也叫NotifyPropertyChanged("Image")。這將迫使WPF更新Image.SourceDepencyProperty,因爲它綁定到Contact.Image。這將返回不同的圖片,因爲Availability已更改。

在我的例子中,我決定存儲圖片。這可能不是您的最佳解決方案。它消耗更多的內存,但可節省處理時間。如果你喜歡,每次重新處理圖像可用性狀態發生變化,則應該將Contact.Image屬性更改爲類似:

public BitmapImage Image 
    { 
     get 
     { 
      switch (this.Availability) 
      { 
       case ContactAvailability.Available: 
        return this._AvailablePicture; 
       case ContactAvailability.Busy: 
        return GetImageWithColorFilter(this._AvailablePicture, Colors.Red); 
       default: 
        throw new NotImplementedException(); 
      } 
     } 
    } 
+0

thanx你的答案,但我有一個問題。 這與圖像處理方面的綁定有何不同。我的意思是每當可用性發生變化時,以這種方式也應該執行圖像處理代碼。我對不對@bouvierr?或者您是否想要爲同一個人聯繫人的每個可用狀態設置多個圖像?如果沒有那麼你的交換機是如何工作的? 以及我對C#和WPF完全陌生,所以可能是我錯了那是我無法弄清楚如何INotifyPropertyChanged和此代碼將工作? 如果你詳細說明這個邏輯如何工作,我會很高興。 –

+0

如果你詳細說明這個邏輯如何工作,我會很高興。 –

+0

如果答案可以幫助你,請upvote :) :) – bouvierr

相關問題