首页 > 其他 > 详细

WPF 根据性别显示图片

时间:2014-02-24 21:49:50      阅读:599      评论:0      收藏:0      [点我收藏+]

先上图!

bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣

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();
        }
    }
}

WPF 根据性别显示图片

原文:http://blog.csdn.net/coolfeiweb/article/details/19768207

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!