0

在我的页面上,我有一个LongListSelector& DatePicker,如下所示。

<phone:LongListSelector ItemsSource="{Binding Categories, Mode=TwoWay}"
                        Name="llsCategories"
                        ItemTemplate="{StaticResource AllCategories}"/>

<toolkit:DatePicker Name="dpDate" Value="{Binding LastModifiedOn,Mode=TwoWay}"/>

这是我为LongListSelector. 注意,GroupName物业

    <DataTemplate x:Key="AllCategories">
        <StackPanel Orientation="Vertical">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions >
                    <ColumnDefinition Width="Auto"></ColumnDefinition>
                </Grid.ColumnDefinitions>

                <RadioButton Content="{Binding CategoryName, Mode=TwoWay}" 
                             IsChecked="{Binding IsCategorySelected, Mode=TwoWay}"
                             GroupName="categoryList"/>
            </Grid>
        </StackPanel>
    </DataTemplate>

LongListSelector 的数据源Categories存在IList<Category>于我的 Viewmodel 中,它已经实现INotifyPropertyChanged

这就是我数据绑定的方式。

在页面的构造函数中:

this.DataContext = App.ViewModel

在 App.xaml 中

private static MainViewModel viewModel = null;
 public static MainViewModel ViewModel
        {
            get
            {
                // Delay creation of the view model until necessary
                if (viewModel == null)
                    viewModel = new MainViewModel();

                return viewModel;
            }
        }

问题:我选择了 LongListSelector 中显示的类别(单选按钮)之一,然后当我完成选择日期时间时,之前勾选的单选按钮未勾选。为什么 ?

编辑: 这是示例代码。您将需要 VS 2012。运行该项目。选择第一个或最后一个单选按钮。然后选择日期。观察以前选择的单选按钮现在未选中。

4

1 回答 1

0

A couple of issues. Your main binding issue is becuase the IsCategorySelected property is on MainViewModel instead of Category (since you're binding the selected property of the Category not MainViewModel).

Additionally, I wouldn't constantly recreate the categories list in MainViewModel.Categories, rather initialize the list once in your constructor, something like:

public MainViewModel()
        {
            _categories = new List<Category>();
            Category c1 = new Category { CategoryName = "Hi" };
            Category c2 = new Category { CategoryName = "Hello" };
            Category c3 = new Category { CategoryName = "Howdy" };
            _categories = new List<Category>();
            _categories.Add(c1);
            _categories.Add(c2);
            _categories.Add(c3);
        }

Hope that helps!

于 2013-05-28T18:48:07.143 回答