2012-08-03 46 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>