关于WPF中Image的Source属性绑定的问题,求高手指点!
本人初学WPF,对其中的数据绑定不能理解,本意想编写一个指示灯程序,当某个值为TRUE是Image加载绿色图片,反之则为红色,但一直搞不定,请高手看看哪出问题了?
Xaml代码:
数据转换器代码:
窗口后台代码:
特别需要高人指点一下Binding.Path到底指什么,看了MSDN和好多资料都没理解。
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和好多资料都没理解。
...全文
相关推荐
WPF 切换图片动画效果Demo
WPF 切换图片动画效果 Demo 在同一个Image控件中,改变Source属性时动画实现
WPF Image 点击事件
WPF 控件 Image 点击事件 1 定义事件 2 为目标控件附加一个模拟的鼠标单击事件
wpf image button
wpf imagebutton 自定义控件 新手 自己写的demo,老手不用来了 c#
MVVM模式下WPF动态绑定展示图片
主要为大家详细介绍了MVVM模式下WPF动态绑定展示图片的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
WPF中显示图片并将路径显示
WPF中利用image控件显示绝对路径下的图片,将绝对路径在textbox显示出来


