2017-04-24 38 views
-1

我想创建一个圆形的“播放”按钮。 我想使用2张照片。 1是默认图片,名称为:“PlayButton.png” 另一个应该在用户点击图片文件夹内名为“PlayButtonPressed.png”的按钮后出现。Databind ImageBrush里面的椭圆里面的一个按钮

我试图使用ImageBrush的ImageSource的简单数据绑定来检查它。 但是Inotifychange propery保持为空。并没有图片看起来。不是程序运行时,而是单击空按钮时。

我该如何使用数据绑定来控制图像源。

这里是我的代码:

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#视图模型:

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#常量值

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财产在类中定义的,你应该设置DataContext这一个实例:

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

梁与所述相同的问题:( –

+0

你看不到根本不存在图像 – mm8

+0

没有看到图像 –

相关问题