【问题标题】:Changing button background image based upon Binding根据 Binding 更改按钮背景图像
【发布时间】:2014-11-19 22:27:03
【问题描述】:

我正在尝试从按钮中获取背景图像,以根据 MVVM 链接对象中的值进行更改。

按钮的默认样式是image1,

对象处理成功后,属性设置为“Success”,按钮图像应变为image2

当对象没有被成功处理时,属性设置为“Error:”并且按钮图像应该变为image3

我尝试绑定到表示文件位置的字符串(直接在 Button.Background 标记中的 ImageBrush 属性 ImageSource 上输入的相同字符串。

将 Uri 返回到该文件也不起作用

尝试使用带有触发器的 Setter 标记,使用时 XAML 中断

并尝试使用对象内的其他属性从中创建 BitmapImage 也不起作用。

这是应用程序中唯一剩下的东西。有人有什么建议吗

【问题讨论】:

  • 你能显示一些代码吗?

标签: windows-phone-8 mvvm binding


【解决方案1】:

我现在找到了一个可行的解决方案。通过从按钮中删除背景,并将以下内容添加到内容中:

      <Button.Content>
         <StackPanel Orientation="Vertical">
            <Image Source="{Binding Path=Background}" Stretch="Fill" Height="100" Width="400" />
            <TextBlock Margin="0,-130,0,0" Height="30" Width="400" HorizontalAlignment="Center"
                 TextAlignment="Center" VerticalAlignment="Center"  Text="{Binding Path=Title}"
                 Foreground="White" FontFamily="/Assets/futura-md.ttf#Futura Md BT"/>
         </StackPanel>
      </Button.Content>

背景定义如下:

    public BitmapImage Background
    {
        get
        {
            return new BitmapImage(new Uri(string.Format( "/Assets/knop_{0}.png",buttoncolor), UriKind.Relative));
        }
    }

    public string ButtonImage 
    { 
        get { return buttonimage; } 
        set 
        { 
           buttonimage = value; 
           RaisePropertyChanged("ButtonImage"); 
           RaisePropertyChanged("Background"); 
        }
    }

现在按钮'背景'会随着ButtonImage的值改变而改变。

这并不完全是我所希望的,但它正在发挥作用。

【讨论】:

    猜你喜欢
    • 2014-01-25
    • 2011-07-16
    • 2013-09-07
    • 2016-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多