先上图!
xaml 代码:
<Window x:Class="Wpf001.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
DataContext="{Binding Main, Source={StaticResource Locator}}"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Converter:ImageConverter x:Key="ImageConverter" xmlns:Converter="clr-namespace:Wpf001.Utility" />
</Window.Resources>
<Grid>
<RadioButton Content="男" IsChecked="True" GroupName="sex" HorizontalAlignment="Left" Margin="40,39,0,0" VerticalAlignment="Top"
Command="{Binding CheckSexCommand}"
CommandParameter="男"/>
<RadioButton Content="女" GroupName="sex" HorizontalAlignment="Left" Margin="133,39,0,0" VerticalAlignment="Top"
Command="{Binding CheckSexCommand}"
CommandParameter="女"/>
<Label x:Name="ImgSex" HorizontalAlignment="Left" Margin="210,39,0,0" VerticalAlignment="Top"
Content="{Binding Sex, Mode=TwoWay}"/>
<Image HorizontalAlignment="Left" Height="155" Margin="31,82,0,0" VerticalAlignment="Top" Width="143"
Source="{Binding ElementName=ImgSex, Path=Content, Converter={StaticResource ImageConverter}}"
/>
</Grid>
</Window>
ViewModel文件夹下的MainViewModel .cs 代码
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
namespace Wpf001.ViewModel
{
public class MainViewModel : ViewModelBase
{
public RelayCommand<string> CheckSexCommand { get; set; }
public MainViewModel()
{
CheckSexCommand = new RelayCommand<string>(CheckSex);
}
private void CheckSex(string str)
{
if (str == "男")
{
Sex = "男";
}
else
{
Sex = "女";
}
}
private string sex = "男";
public string Sex
{
get { return sex; }
set
{
sex = value;
RaisePropertyChanged("Sex");
}
}
}
}
Utility 文件夹下的ImageConverter.cs 代码:
using System;
using System.Windows.Data;
using System.Windows.Media.Imaging;
namespace Wpf001.Utility
{
class ImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return new BitmapImage(new Uri(string.Format("/Image/{0}.jpg",value), UriKind.Relative));
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
原文:http://blog.csdn.net/coolfeiweb/article/details/19768207