2017-04-24 5 views
-1

円の「再生」ボタンを作成したいと思います。 2枚の写真を使いたいです。 1は、「PlayButton.png」 と呼ばれるデフォルトの画像で、ユーザーがImagesフォルダ内の "PlayButtonPressed.png"というボタンをクリックした後に表示されるはずです。ボタンの内側にあるImageBrush内のelipse内部のデータバインド

ImageBrushのImageSourceにこれをチェックするための簡単なデータバインディングを使用しようとしました。 しかし、Inotifychangeプロパティはnullのままです。写真は見られません。プログラムが実行されたときではなく、空のボタンをクリックしたときではありません。

Image Sourceを制御するためのデータバインディングを行うにはどうすればよいですか。ここ

は私のコードです:

XAML:

<Window x:Class="COMSimulator.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:Converters="clr-namespace:COMSimulator" 
<Button x:Name="btnStart" Grid.Column="0" Grid.Row="2" 
    HorizontalAlignment="Center" VerticalAlignment="Center" Width="50" 
    Height="50" Click="btnStart_Click"> 
    <Button.Template> 
     <ControlTemplate> 
      <Ellipse Height="50" Width="50"> 
       <Ellipse.Fill> 
        <ImageBrush ImageSource="{Binding Path=ImagePlayButton}"></ImageBrush> 
       </Ellipse.Fill> 
      </Ellipse> 
     </ControlTemplate> 
    </Button.Template> 
</Button> 
</Window > 

のC#:

namespace COMSimulator 
{ 


public partial class MainWindow : Window 
{ 


    public viewModeImage VMI { get; set; } 
    public MainWindow() 
    {    
     InitializeComponent(); //init componenets 
     VMI = new viewModeImage();    
     DataContext = this;    
    VMI.ImagePlayButton = Consts.IMAGE_PLAY_BUTTON; 
    } 
} 
} 

C#のViewModel:

namespace COMSimulator 
{ 
public class viewModeImage:INotifyPropertyChanged 
{ 


    private string _imagePlayButton; 

    /// <summary> 
    /// this method init all window Items to relavent Data 
    /// </summary> 
    public string ImagePlayButton 
    { 
     get 
     { 
      return _imagePlayButton; 
     } 
     set 
     { 
      _imagePlayButton = value; 
      OnProperyChanged("ImagePlayButton"); 
     } 

    } 
    public event PropertyChangedEventHandler PropertyChanged; 

    private void OnProperyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 
} 

C#のConst値

public static class Consts 
{ 

    public const string IMAGE_PLAY_BUTTON [email protected]"Images\PlayButton.png"; 
    public const string IMAGE_PLAY_BUTTON_PRESSED = @"Images\PlayButtonPressed.png"; 

} 

答えて

1

自体にウィンドウのDataContextを設定してください:

public MainWindow() 
{    
    InitializeComponent(); //init componenets 
    ImagePlayButton= @"Images\PlayButton.png"; 
    DataContext = this; 
} 

BindingPathは、要素のDataContextのプロパティ名を参照します。これは、DataContextを、バインディングが機能するように、ImagePlayButtonプロパティが定義されているクラスに設定する必要があることを意味します。

編集:ImagePlayButtonプロパティは別のクラスで定義されているので、あなたはこの1つのインスタンスにDataContextを設定する必要があります。同じ問題を持つシル

public MainWindow() 
{    
    InitializeComponent(); //init componenets 
    VMI = new viewModeImage(); 
    VMI.ImagePlayButton = Consts.IMAGE_PLAY_BUTTON;     
    this.btnStart.DataContext = VMI; 
} 
+0

を:( –

+0

あなたが表示されません画像すべてで – mm8

+0

番号画像がまったく表示されない –

関連する問題