没有为不可见的项目设置绑定

时间:2016-05-04 13:57:08

标签: windows-phone-8.1 winrt-xaml datacontext

对不起,如果这是一个愚蠢的问题。我正在维护这个疯狂的Windows Phone 8.1 RT动态应用程序,我没有写。它将大量内容加载到DataContext。屏幕上看不到的东西似乎没有得到他们的DataContext。当我离开表格时,事件会发生。我需要做些什么来解决这个问题?即使它滚动到视图中也不会加载。如果我抬起头并单击后退按钮,我会看到该字段在进入上一页之前填充。

编辑 - 这是一些代码:

        <DataTemplate x:Key="com.somecompany.BarcodeboxRenderer">
            <Grid HorizontalAlignment="Stretch" DataContext="{Binding Item1}">
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <TextBlock Text="{Binding Label}" HorizontalAlignment="Stretch" Style="{StaticResource labelstyle}" Grid.Row="0" />
                <somecompany:BarcodeBox IsReadOnly="{Binding DisplayOnly}" somecompany:DynamicDataBindingPath.BindingPath="{Binding FieldId}" Grid.Row="1" />
            </Grid>
        </DataTemplate>

That calls this but only if it's visible:

    public sealed class DynamicDataBindingPath:FrameworkElement
    {
        public static readonly DependencyProperty BindingPathProperty = DependencyProperty.RegisterAttached("BindingPath", typeof(string), typeof(DynamicDataBindingPath), new PropertyMetadata("", OnBindingPathPropertyChanged));

        public static string GetBindingPath(FrameworkElement target)
        {
            try
            {
                return (string)target.GetValue(BindingPathProperty);
            }
            catch (Exception)
            {
                return string.Empty;
            }
        }

        public static void SetBindingPath(FrameworkElement target, string value)
        {
            target.SetValue(BindingPathProperty, value);
            target.Loaded += Target_Loaded;
        }

        private static void Target_Loaded(object sender, RoutedEventArgs e) {
            var target = (FrameworkElement)sender;
        if (target is ...) { ... }
        else if (target is DatePicker)
        {
            int startingletterindex = value.IndexOf('.') + 1;
            string pathtobindto = "obj." + Char.ToUpper(value[startingletterindex]) + value.Substring((startingletterindex + 1));
            Binding newbind = new Binding();
            newbind.Path = new PropertyPath(pathtobindto);
            var contextsrc = findRealDataContext(target);
            newbind.Source = contextsrc.DataContext;
            newbind.Mode = BindingMode.TwoWay;
            (target as DatePicker).SetBinding(DatePicker.DateProperty, newbind);
        }
        else if (...)
     }

1 个答案:

答案 0 :(得分:0)

我转向了ListView虚拟化,它现在可以使用了。我不需要它,因为永远不会有超过几行。