是否可以在页面的资源字典中使用x:bind样式绑定?

时间:2016-06-06 17:42:46

标签: c# win-universal-app resourcedictionary ivalueconverter xbind

我有以下情况......

<Page.Resources>
    <converters:EnumToBoolean x:Key="Empty" Expected="Empty"/>
    <converters:EnumToBoolean x:Key="Full"  Expected="Full"/>
</Page.Resources>

.
.
.

<SomeControl IsChecked="{x:Bind ViewModel.State, Converter={StaticResource Empty}, Mode=TwoWay}"/> 
<SomeControl IsChecked="{x:Bind ViewModel.State, Converter={StaticResource Full},  Mode=TwoWay}"/> 

EnumToBooleanIValueConverter,可将c#enum转换为boolean

它使用Expected值来确定ViewModel.State是真还是假。这是Convert的{​​{1}}部分,它有效,没什么特别的。

但是,如果'boolean'值为false,则IValueConverter需要返回ConvertBack的原始值作为结果。按预期返回“null”会导致运行时异常。

我尝试了两种方法将ViewModel.State值转换为转换器的Original函数:

a)添加了另一个字段ConvertBack并尝试绑定它

Original

这会编译并运行,但会导致运行时异常;因为它试图在转换器实例化之前绑定到转换器

b)尝试将其作为带有嵌入式绑定的<converters:EnumToBoolean x:Key="Empty" Expected="Empty" Original="{x:Bind ViewModel.State, Mode=OneWay}"/> <converters:EnumToBoolean x:Key="Full" Expected="Full" Original="{x:Bind ViewModel.State, Mode=OneWay}"/> 传递

ConverterParameter

根本不编译;如果没有错误代码或此处或谷歌上的任何点击,它会失败并出现非描述性错误...“无法跨行边界划分事件值

有没有人建议如何使这些方案中的任何一个工作?

注意 - 我的临时解决方案,远非理想,是制作转换器的 <SomeControl IsChecked="{x:Bind ViewModel.State, Converter={StaticResource Empty}, ConverterParameter={x:Bind ViewModel.WallPainter, Mode=OneWay}, Mode=TwoWay}"/> <SomeControl IsChecked="{x:Bind ViewModel.State, Converter={StaticResource Full}, ConverterParameter={x:Bind ViewModel.WallPainter, Mode=OneWay}, Mode=TwoWay}"/> 字段 {{1 并在后面的代码中设置其getter Original 。我不关心这个选项,因为它会破坏绑定实现策略。

0 个答案:

没有答案