ChangePropertyAction [uwp] 上的 Change IsChecked 属性
Change IsChecked property on ChangePropertyAction [uwp]
我需要使用 ChangePropertyAction 在 XAML 中单击一个简单的按钮来更改单选按钮的 IsChecked 属性 .
我可以通过此方法更改单选按钮的每个 属性(内容、可见性等),但 IsChecked 除外,它会给出未命名的错误。
<Button x:Name="button1">
<Interactivity:Interaction.Behaviors>
<Core:EventTriggerBehavior EventName="Click" SourceObject="{Binding ElementName=button1}">
<Core:ChangePropertyAction TargetObject="{Binding ElementName=radio1}" PropertyName="IsChecked" Value="false"/>
</Core:EventTriggerBehavior>
</Interactivity:Interaction.Behaviors>
</Button>
请问是什么原因和解决方法?谢谢
问题似乎是由于这个原因引起的,选中的复选框 属性 继承自其基础 class ToggleButton。它的值是 Nullable(详见此 doc)。这就是它报告如下异常的原因:
Windows.UI.Xaml.Markup.XamlParseException: The text associated with this error code could not be found.
name expected [Line: 1 Position: 175]
at Windows.UI.Xaml.Markup.XamlReader.Load(String xaml)
at Microsoft.Xaml.Interactions.Core.TypeConverterHelper.Convert(String value, String destinationTypeFullName)
要解决这个问题,尝试自己创建一个转换器:
public class NullReaderConverter:IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
然后在您的 XAML 上尝试以下操作:
<Grid>
<Grid.Resources>
<local:NullReaderConverter x:Name="myconverter"/>
<x:Boolean x:Key="falsevalue">false</x:Boolean>
</Grid.Resources>
<RelativePanel>
<CheckBox Content="test1" x:Name="radio1" IsChecked="True"></CheckBox>
<Button x:Name="button1" Content="change" RelativePanel.Below="radio1">
<Interactivity:Interaction.Behaviors>
<Interactions:EventTriggerBehavior EventName="Click" SourceObject="{Binding ElementName=button1}">
<Interactions:ChangePropertyAction TargetObject="{Binding ElementName=radio1}" PropertyName="IsChecked" Value="{Binding Converter={StaticResource myconverter},Source={StaticResource falsevalue}}"/>
</Interactions:EventTriggerBehavior>
</Interactivity:Interaction.Behaviors>
</Button>
</RelativePanel>
</Grid>
我需要使用 ChangePropertyAction 在 XAML 中单击一个简单的按钮来更改单选按钮的 IsChecked 属性 .
我可以通过此方法更改单选按钮的每个 属性(内容、可见性等),但 IsChecked 除外,它会给出未命名的错误。
<Button x:Name="button1">
<Interactivity:Interaction.Behaviors>
<Core:EventTriggerBehavior EventName="Click" SourceObject="{Binding ElementName=button1}">
<Core:ChangePropertyAction TargetObject="{Binding ElementName=radio1}" PropertyName="IsChecked" Value="false"/>
</Core:EventTriggerBehavior>
</Interactivity:Interaction.Behaviors>
</Button>
请问是什么原因和解决方法?谢谢
问题似乎是由于这个原因引起的,选中的复选框 属性 继承自其基础 class ToggleButton。它的值是 Nullable(详见此 doc)。这就是它报告如下异常的原因:
Windows.UI.Xaml.Markup.XamlParseException: The text associated with this error code could not be found. name expected [Line: 1 Position: 175] at Windows.UI.Xaml.Markup.XamlReader.Load(String xaml) at Microsoft.Xaml.Interactions.Core.TypeConverterHelper.Convert(String value, String destinationTypeFullName)
要解决这个问题,尝试自己创建一个转换器:
public class NullReaderConverter:IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
然后在您的 XAML 上尝试以下操作:
<Grid>
<Grid.Resources>
<local:NullReaderConverter x:Name="myconverter"/>
<x:Boolean x:Key="falsevalue">false</x:Boolean>
</Grid.Resources>
<RelativePanel>
<CheckBox Content="test1" x:Name="radio1" IsChecked="True"></CheckBox>
<Button x:Name="button1" Content="change" RelativePanel.Below="radio1">
<Interactivity:Interaction.Behaviors>
<Interactions:EventTriggerBehavior EventName="Click" SourceObject="{Binding ElementName=button1}">
<Interactions:ChangePropertyAction TargetObject="{Binding ElementName=radio1}" PropertyName="IsChecked" Value="{Binding Converter={StaticResource myconverter},Source={StaticResource falsevalue}}"/>
</Interactions:EventTriggerBehavior>
</Interactivity:Interaction.Behaviors>
</Button>
</RelativePanel>
</Grid>