最近做项目的时候,碰到一个属性可能有多个值(多的能有5、6个)的问题,UI设计的图是用多个RadioButton展示,这种属性在页面上有很多,这样以前每个RadioButton定义一个属性的情况就不太好了,于是想到用一个转换器来转换,转换器的参数设置成对应的代码值。
转换器如下:
class FwlxConverter:IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value!=null&&!string.IsNullOrEmpty(value.ToString()))
{
if (value.ToString()==parameter.ToString())
{
return true;
}
}
return false;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value != null && !string.IsNullOrEmpty(value.ToString()))
{
if ((bool)value)
{
return parameter.ToString();
}
}
return null;
}
但是有一个问题是,当有ViewModel通知界面的时候,其中有的未被选中的值可能会覆盖属性值。一直没想到什么好办法。今天在网上找了一下,看到一篇文章,恍然大悟。。。,属性set值的时候可以不处理空值。
下面附上这篇文章:(转载至:http://www.it165.net/pro/html/201305/5996.html (地址是我看到这篇文章的网址,如果不是原作者发的,请原作者告知))
<
Window
x:Class
=
"RadioButtonBindingDemo.MainWindow"
03.
xmlns:x
=
"http://schemas.microsoft.com/winfx/2006/xaml"
04.
xmlns:local
=
"clr-namespace:ViewModel;assembly=ViewModel"
05.
Title
=
"MainWindow"
Height
=
"350"
Width
=
"525"
>
06.
<
Window.DataContext
>
07.
<
local:ViewModelClass
/>
08.
</
Window.DataContext
>
09.
<
Window.Resources
>
10.
<
local:MyConverter
x:Key
=
"MyConverter"
/>
11.
</
Window.Resources
>
12.
<
Grid
>
13.
<
StackPanel
Width
=
"156"
Orientation
=
"Horizontal"
>
14.
<
RadioButton
GroupName
=
"name"
IsChecked
=
"{Binding Path=CurrentOption, Mode=TwoWay, Converter={StaticResource MyConverter}, ConverterParameter=0}"
Content
=
"Option A"
Width
=
"45"
/>
15.
<
RadioButton
GroupName
=
"name"
IsChecked
=
"{Binding Path=CurrentOption, Mode=TwoWay, Converter={StaticResource MyConverter}, ConverterParameter=1}"
Content
=
"Option B"
Width
=
"45"
/>
16.
<
RadioButton
GroupName
=
"name"
IsChecked
=
"{Binding Path=CurrentOption, Mode=TwoWay, Converter={StaticResource MyConverter}, ConverterParameter=2}"
Content
=
"Option C"
Width
=
"60"
/>
17.
18.
</
StackPanel
>
19.
<
Button
Content
=
"Show your selection"
Command
=
"{Binding Path=CmdShowMessage}"
HorizontalAlignment
=
"Left"
Margin
=
"342,62,0,0"
VerticalAlignment
=
"Top"
Width
=
"128"
/>
20.
</
Grid
>
21.
</
Window
>
三个 RadioButton 的GroupName 设为一样的名字,IsChecked 也 Binding 到同一个 Property,Mode=TwoWay,最后再加上 Command 和 CommandParameter ,并在不同的 RadioButton 指定不同的 CommandParameter 好让 ViewModel 区别选到哪一个 RadioButton.
下面是Converter:
01.
using
System;
02.
using
System.Collections.Generic;
03.
using
System.Linq;
04.
using
System.Text;
05.
using
System.Windows.Data;
06.
07.
namespace
ViewModel
08.
{
09.
public
class
MyConverter : IValueConverter
10.
{
11.
public
object
Convert(
object
value, Type targetType,
object
parameter, System.Globalization.CultureInfo culture)
12.
{
13.
if
(value ==
null
|| parameter ==
null
)
14.
return
false
;
15.
string
checkvalue = value.ToString();
16.
string
targetvalue = parameter.ToString();
17.
bool
r = checkvalue.Equals(targetvalue,
18.
StringComparison.InvariantCultureIgnoreCase);
19.
return
r;
20.
}
21.
22.
public
object
ConvertBack(
object
value, Type targetType,
object
parameter, System.Globalization.CultureInfo culture)
23.
{
24.
if
(value ==
null
|| parameter ==
null
)
25.
return
null
;
26.
bool
usevalue = (
bool
)value;
27.
28.
if
(usevalue)
29.
return
parameter.ToString();
30.
31.
return
null
;
32.
}
33.
}
34.
}
利用 Parameter 来判断所选的 RadioButton 。
再配合一下 Command :
01.
using
System;
02.
using
System.Collections.Generic;
03.
using
System.Linq;
04.
using
System.Text;
05.
using
System.Windows.Input;
06.
07.
namespace
ViewModel
08.
{
09.
public
class
ShowMsgCmdClass : ICommand
10.
{
11.
ViewModelClass vm;
12.
public
ShowMsgCmdClass(ViewModelClass fvm)
13.
{
14.
vm = fvm;
15.
}
16.
public
bool
CanExecute(
object
parameter)
17.
{
18.
return
true
;
19.
}
20.
21.
public
event
EventHandler CanExecuteChanged;
22.
23.
public
void
Execute(
object
parameter)
24.
{
25.
vm.ShowMsg();
26.
}
27.
}
28.
}
连接 ViewModel :
01.
using
System;
02.
using
System.Collections.Generic;
03.
using
System.ComponentModel;
04.
using
System.Linq;
05.
using
System.Text;
06.
using
System.Windows.Input;
07.
08.
namespace
ViewModel
09.
{
10.
public
class
ViewModelClass : INotifyPropertyChanged
11.
{
12.
string
currentOption;
13.
14.
public
string
CurrentOption
15.
{
16.
get
17.
{
18.
return
currentOption;
19.
}
20.
set
21.
{
22.
if
(value !=
null
)
//要判断一下是否为 null,否则选了A,又选B时,最后一个回传的会是A的值,这样就抓不到了。www.it165.net
23.
currentOption = value;
24.
}
25.
}
26.
27.
ShowMsgCmdClass cmdShowMessage;
28.
29.
public
ShowMsgCmdClass CmdShowMessage
30.
{
31.
get
{
return
cmdShowMessage; }
32.
set
{ cmdShowMessage = value; }
33.
}
34.
public
ViewModelClass()
35.
{
36.
cmdShowMessage =
new
ShowMsgCmdClass(
this
);
37.
}
38.
39.
public
event
PropertyChangedEventHandler PropertyChanged;
40.
public
void
OnPropertyChanged(
string
prop)
41.
{
42.
PropertyChangedEventHandler handler = PropertyChanged;
43.
if
(handler !=
null
)
44.
handler(
this
,
new
PropertyChangedEventArgs(prop));
45.
}
46.
47.
public
void
ShowMsg()
48.
{
49.
System.Windows.MessageBox.Show(
"You Select "
+ currentOption);
50.
}
51.
}
52.
}