如何具体值传递给转换器参数α [英] How to pass specific value to the converter parameter?
问题描述
我创建了一个类的人,看起来是这样的:
I have created a class Person that looks like this:
public class Person
{
public enum GenderType
{
Female,
Male
}
public string Name
{
get; set;
}
public GenderType? Gender
{
get; set;
}
}
接下来,我创建了将要Person类型$ P $ psent对象的数据模板。
下面是XAML code:
Next, I created data template that is going to present objects of type Person.
Here's XAML code:
<DataTemplate
x:Key="personTemplate"
DataType="{x:Type model:Person}">
<StackPanel>
<RadioButton
Content="Female"
IsChecked="{Binding Path=Gender,
Converter={StaticResource genderConverter},
ConverterParameter=???}"/>
<RadioButton
Content="Male"
IsChecked="{Binding Path=Gender,
Converter={StaticResource genderConverter},
ConverterParameter=???}"/>
<RadioButton
Content="Not specified"
IsChecked="{Binding Path=Gender,
Converter={StaticResource genderConverter},
ConverterParameter=???}"/>
</StackPanel>
</DataTemplate>
当然, ???
S在了code都不行:)问题是,我想创建一个中 genderConverter
转换器,它在给定值,即比较 personObject.Gender
,对给定的 Person.GenderType
中的参数提供的值,并返回真
如果值匹配。
Of course the ???
s in the code won't work :) The problem is that I want to create a genderConverter
converter which will compare the given value, i.e. personObject.Gender
, against the given Person.GenderType
value provided in the parameter and return true
if the values match.
我不知道如何使变频器参数通 Person.GenderType.Female
, Person.GenderType.Male
和空
,为第一,第二和第三个单选按钮,分别为。
I don't know how to make the converter parameter pass Person.GenderType.Female
, Person.GenderType.Male
and null
, for the first, second and third radio button, respectively.
推荐答案
尝试使用 X:静态
标记扩展:
<RadioButton
Content="Female"
IsChecked="{Binding Path=Gender,
Converter={StaticResource genderConverter},
ConverterParameter={x:Static model:GenderType.Female}}"/>
或者,你可以只通过一个字符串,并使用 Enum.Parse
来的字符串转换为转换器中的枚举类型:
OR, you could just pass a string and use Enum.Parse
to convert that string to the enum type in the converter:
<RadioButton
Content="Female"
IsChecked="{Binding Path=Gender,
Converter={StaticResource genderConverter},
ConverterParameter=Female}"/>
-
GenderType gender = (GenderType)Enum.Parse(typeof(GenderType), parameter.ToString());
这篇关于如何具体值传递给转换器参数α的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!