关于WPF中Image的Source属性绑定的问题,求高手指点!

supersars 2014-10-05 06:01:09
本人初学WPF,对其中的数据绑定不能理解,本意想编写一个指示灯程序,当某个值为TRUE是Image加载绿色图片,反之则为红色,但一直搞不定,请高手看看哪出问题了?
Xaml代码:

<Window x:Class="WpfApplication6.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="clr-namespace:WpfApplication6"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<converters:BoolToSource x:Key="booltosource"></converters:BoolToSource>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="75"/>
</Grid.RowDefinitions>
<Image x:Name="img" Grid.Row="0" Grid.ColumnSpan="2" Source="{Binding Path=URL, Converter={StaticResource booltosource}}"/>
<TextBox x:Name="tb" Grid.Column="0" Grid.Row="1"></TextBox>
<Button x:Name="bt" Content="点击" Grid.Column="1" Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Center" Width="100" Height="50" Click="bt_Click"></Button>
</Grid>
</Window>

数据转换器代码:

using System;
using System.Collections.Generic;
using System.Windows.Data;
using System.Linq;
using System.Text;
using System.Windows.Media.Imaging;

namespace WpfApplication6
{
[ValueConversion(typeof(bool), typeof(BitmapImage))]
class BoolToSource:IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
bool temp =(bool) value;
if (temp)
return new BitmapImage(new Uri("Images\\red.ico", UriKind.Relative));
else
return new BitmapImage(new Uri("Images\\green.ico", UriKind.Relative));
}


public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}

}
}

窗口后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication6
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
Binding bd = new Binding();
bool ss;
public MainWindow()
{
InitializeComponent();
ss=true ;
bd.Source = ss;
bd.Path = new PropertyPath("URL");
this.img.SetBinding(Image.SourceProperty, bd);
}

private void bt_Click(object sender, RoutedEventArgs e)
{

}
}
}

特别需要高人指点一下Binding.Path到底指什么,看了MSDN和好多资料都没理解。
...全文
958 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 2 楼 supersars 的回复:
我试了试,还是不行,恕我愚笨,又是初学者,望高手改改这段代码,让他实现我预期的功能。
跟你改了,下次你估计还不会。应该是这样吧

return new BitmapImage(new Uri(string.Format(@"\Image\{0}.png",eIcon), UriKind.RelativeOrAbsolute));
自己在Converter类里打个断点,走走看。
supersars 2014-10-08
  • 打赏
  • 举报
回复
引用 3 楼 duanzi_peng 的回复:
[quote=引用 2 楼 supersars 的回复:] 我试了试,还是不行,恕我愚笨,又是初学者,望高手改改这段代码,让他实现我预期的功能。
跟你改了,下次你估计还不会。应该是这样吧

return new BitmapImage(new Uri(string.Format(@"\Image\{0}.png",eIcon), UriKind.RelativeOrAbsolute));
自己在Converter类里打个断点,走走看。[/quote] 谢谢你的帮助,我已经搞定了,分数已奖励。
  • 打赏
  • 举报
回复
晕死,你这样是写了2次绑定;xaml一次,后台一次。 Binding.Path 就是 给你的的binding属性设置path,指定路径。只不过是 放到了后台,是一种书写形式罢了。
supersars 2014-10-06
  • 打赏
  • 举报
回复
我试了试,还是不行,恕我愚笨,又是初学者,望高手改改这段代码,让他实现我预期的功能。

8,756

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧