【问题标题】:ItemsControl List<string>-Binding Won't WorkItemsControl List<string>-绑定不起作用
【发布时间】:2015-12-31 03:16:38
【问题描述】:

我使用this 作为模板,但在 Windows Phone 模拟器中没有显示任何内容。我正在尝试将字符串列表绑定到ItemsControl。当我这样做时不会工作。我也在尝试使用StackPanel,但我删除了以尝试使其正常工作。

PivotPage.xaml

<Page
    x:Class="WinPhone8__Practice.PivotPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:WinPhone8__Practice"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:data="using:WinPhone8__Practice.Data"
    mc:Ignorable="d"
    DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Page.Transitions>
        <TransitionCollection>
            <NavigationThemeTransition>
                <NavigationThemeTransition.DefaultNavigationTransitionInfo>
                    <CommonNavigationTransitionInfo IsStaggeringEnabled="True"/>
                </NavigationThemeTransition.DefaultNavigationTransitionInfo>
            </NavigationThemeTransition>
        </TransitionCollection>
    </Page.Transitions>

    <Grid>
        <Pivot x:Uid="Pivot" Title="MY APPLICATION" x:Name="pivot" CommonNavigationTransitionInfo.IsStaggerElement="True">
            <!--Pivot item one-->
            <PivotItem
                x:Uid="PivotItem1"
                Margin="19,14.5,0,0"
                CommonNavigationTransitionInfo.IsStaggerElement="True">
                <!--Double line list with text wrapping-->
                <ItemsControl ItemsSource="{Binding strings}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding}"/>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </PivotItem>
        </Pivot>
    </Grid>
</Page>

PivotPage.xaml.cs

 public sealed partial class PivotPage : Page
    {
        private readonly NavigationHelper navigationHelper;
        public List<string> strings { get; private set; }
        public PivotPage()
        {
            this.InitializeComponent();
            strings = new List<string>() { "Yes", "No", "Maybe", "I don't know", "Can you repeat the question?" };

            this.NavigationCacheMode = NavigationCacheMode.Required;

            this.navigationHelper = new NavigationHelper(this);
            this.navigationHelper.LoadState += this.NavigationHelper_LoadState;
            this.navigationHelper.SaveState += this.NavigationHelper_SaveState;
        }

        /// <summary>
        /// Gets the <see cref="NavigationHelper"/> associated with this <see cref="Page"/>.
        /// </summary>
        public NavigationHelper NavigationHelper
        {
            get { return this.navigationHelper; }
        }

        private async void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
        {
            await DoNothing();
        }

        private void NavigationHelper_SaveState(object sender, SaveStateEventArgs e)
        {
            // TODO: Save the unique state of the page here.
        }

        private Task DoNothing() { return new Task(new Action(() => { })); }
    }

【问题讨论】:

    标签: c# xaml binding windows-phone-8.1 itemscontrol


    【解决方案1】:

    为了使其工作,在构造函数中的this.InitializeComponent(); 之后添加this.DataContext=this;

    为什么需要它

    1. 仅当您设置 DataContext 时,绑定才会起作用;
    2. DataContext的默认值为null;
    3. 通过使用此 this.DataContext=this;,您将 DataContext 初始化为同一类。

    【讨论】:

    • 抱歉,您能在 XAML 代码中向我展示同样的内容吗?几乎没有人会这样表示。
    【解决方案2】:
    1. 首先删除 XAML 中的 ItemsSource="{Binding strings}"
    2. 当您使用 {Binding something} 时,必须与您的资源同名
    3. 最好的方法是用你的道具创建一个类模型

      public class Model { public string Names{ get; set; } }

    4. 在你后面的代码中创建你的类ModelList

      public List&lt;Model&gt; model = new List&lt;Model&gt;();

    5. 创建一个填充字段的方法。

      public void ListModel() { model.Add(new Model { Names = "Hello" }); }

    6. 在您的 Main void 中,您调用 ListModel();PivotItem1.ItemsSource = model

    7. 您的 XAML 将是:

      <Pivot x:Uid="Pivot" Title="MY APPLICATION" x:Name="pivot" CommonNavigationTransitionInfo.IsStaggerElement="True"> <!--Pivot item one--> <PivotItem x:Uid="PivotItem1" Margin="19,14.5,0,0" ItemsSource={Biding} CommonNavigationTransitionInfo.IsStaggerElement="True"> <!--Double line list with text wrapping--> <ItemsControl> <ItemsControl.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Names}" Foreground = "Black"/> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> </PivotItem> </Pivot>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-17
      • 1970-01-01
      • 2015-12-09
      相关资源
      最近更新 更多