2012-08-03 43 views
2

在下面的代码中,转换器中没有断点。单击单选按钮不会做任何事情来更改活动控件。这就像IsChecked甚至不会触发更改事件。有任何想法吗?这是WinRT代码。试图使用WinRT绑定到IsChecked

<Page 
    x:Class="TestBinding.MainPage" 
    IsTabStop="false" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:TestBinding" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d"> 
    <Page.Resources> 
     <local:EqualsToBoolConverter x:Key="equalsToBoolConverter"/> 
    </Page.Resources> 
<Page.TopAppBar> 
     <AppBar IsOpen="True" IsSticky="True"> 
      <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Center"> 
       <RadioButton 
        Style="{StaticResource TextRadioButtonStyle}" 
        Name="goItem1" 
        IsChecked="{Binding ElementName=flipView,Path=SelectedItem,Converter={StaticResource equalsToBoolConverter}, ConverterParameter={Binding ElementName=item1}, Mode=TwoWay}">Go 1</RadioButton> 
       <RadioButton 
        Style="{StaticResource TextRadioButtonStyle}" 
        Name="goItem2" 
        IsChecked="{Binding ElementName=flipView,Path=SelectedItem,Converter={StaticResource equalsToBoolConverter}, ConverterParameter={Binding ElementName=item2}, Mode=TwoWay}">Go 2</RadioButton> 
      </StackPanel> 
     </AppBar> 
    </Page.TopAppBar> 
    <FlipView 
     Name="flipView" 
     Background="{StaticResource ApplicationPageBackgroundThemeBrush}" 
     Margin="100" 
     Padding="10" 
     SelectedIndex="0"> 
     <FlipViewItem Name="item1"> 
      <TextBlock Text="item1"/> 
     </FlipViewItem> 
     <FlipViewItem Name="item2"> 
      <TextBlock Text="item2"/> 
     </FlipViewItem> 
    </FlipView> 
</Page> 
+0

它可以帮助看看调试输出面板。绑定错误将在那里报告。 – ollb 2012-08-03 08:45:17

+1

调试输出面板中不报告绑定错误。这是WinRT,他们有问题。 – Brannon 2012-08-03 16:19:45

+0

你检查了吗?我每天使用WinRT + C++/CX工作,调试输出通常有助于指向正确的方向,当绑定错误或绑定类型不匹配时等。WinRT似乎有一个默默的习惯忽略绑定的异常/问题并仅通过调试输出报告它们(至少在C++/CX中)。所以这通常是我看到的第一个地方,当绑定似乎不起作用或转换器不能执行时。也许你还应该发布转换器源代码? – ollb 2012-08-03 17:32:05

回答

1

我无法在RadioButton.IsChecked中声明任何绑定。但是,通过颠倒这个问题,我确实得到了这个结果。 FlipViewItem上有一个IsSelected属性。我成功地用这种方法:

IsSelected="{Binding ElementName=radioButton1,Path=IsChecked,Mode=TwoWay}" 
0

不知道你的转换器做什么,但IsChecked共同的问题是,它不是一个布尔值,而是一个bool?Nullable<bool>)。这是你的转换器需要/返回吗?

+0

我的转换器只是返回Equals(值,参数)。 Convert方法本身返回对象,所以我认为它会传播到布尔?正好。将它投射到布尔?没有区别。就像我上面提到的,转换器本身从未被调用过。 – Brannon 2012-08-03 14:03:54

+0

但它如何转换回来 - IsChecked flipView.SelectedItem? Mayb我昨晚没有睡够,但我看不出这是如何与绑定。如果只是做一些你想用一些好的旧代码做什么? – 2012-08-03 15:31:04

0

那么,这不是布尔?你需要的。看到(这工作正常):

<Grid Height="150"> 
    <Grid.Resources> 
     <x:Boolean x:Key="MyBool">true</x:Boolean> 
    </Grid.Resources> 
    <StackPanel> 
     <CheckBox IsChecked="true" Content="Hello World" /> 
     <CheckBox IsChecked="{StaticResource MyBool}" Content="Hello World" /> 
    </StackPanel> 
</Grid> 

您的问题是,转换器参数不能绑定值。

+0

转换器参数的良好调用。我将代码更改为使用SelectedIndex而不是SelectedItem。然后我输入整数。唉,它仍然没有做任何事情;转换器命中我没有得到任何断点。我在输出窗口中什么也没有。 – Brannon 2012-08-15 19:21:55

0

This MSDN Blog描述了一种技术,通过它可以使用绑定ConverterParameters。

重点摘录:

ConverterParameter不是depency属性,但一个“简单”的对象。在这种情况下,你不能使用绑定。所有XAML平台都有这种副作用:WP7-8,Silverlight,WPF,当然还有WinRT。

的想法是我的转换器,而不是使用ConverterParameter创建一个依赖属性:为了能够创建一个DP,您的转换器必须自DependencyObject继承:

public class DistanceConverter : DependencyObject, IValueConverter 
{ 
    public UserViewModel CurrentUser 
    { 
     get { return (UserViewModel) GetValue(CurrentUserProperty); } 
     set { SetValue(CurrentUserProperty, value); } 
    } 

    public static readonly DependencyProperty CurrentUserProperty = 
     DependencyProperty.Register("CurrentUser", 
            typeof (UserViewModel), 
            typeof (DistanceConverter), 
            new PropertyMetadata(null)); 

    public object Convert(object value, Type targetType, object parameter, string language) 
    { 
     throw new NotImplementedException(); 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, string language) 
    { 
     throw new NotImplementedException(); 
    } 
} 

然后,我声明我的转换器,我的网页资源:

<common:LayoutAwarePage.Resources> 
    <conv:DistanceConverter x:Key="DistanceConverter" 
    CurrentUser="{Binding User}" 
    CurrentItem="{Binding CurrentItem}" 
    MaxWidthAvailable="450" /> 
</common:LayoutAwarePage.Resources> 

最后,我把我的转换器在我的项目模板:

<Rectangle HorizontalAlignment="Left" VerticalAlignment="Center" 
      Fill="#00FF84" Margin="10,0" Height="10" 
      Width="{Binding Path=CurrentItem.Distance, 
Converter={StaticResource DistanceConverter}}"> 
</Rectangle>