UWP c# 绑定到 ItemsPanel/自定义面板(模板) [英] UWP c# Binding to ItemsPanel / Custom Panel (Template)

查看:42
本文介绍了UWP c# 绑定到 ItemsPanel/自定义面板(模板)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我终于设法用我自己的面板创建了一个 GridView.布局很好.现在我希望能够将我的 ViewModel 的值绑定到我自己的面板.你能帮我做这个吗.目前我正在更改页面后面代码中的值,我不喜欢...

I finally managed to create a GridView with my own Panel. The Layout is fine. Now I want to be able to Bind Values of my ViewModel to my own Panel. Can you help me doing this. For the moment I'm changing the values in the code behind of the page, which I don't like...

目前我正在尝试执行 x:Bind,但出现错误:未将对象引用设置为对象的实例."我不知道正常"绑定是否会有所帮助.我确实尝试过,但没有成功.

For the moment I'm trying to do the x:Bind and I get an error: "Object reference not set to an instance of an object." I don't know if "normal" Binding will help. I did try it but with no success.

  1. 自定义面板

  1. Custom Panel

public class PRGD010_GridViewPanel : Panel
{
public int NumberRowsOrColumns
{
    get { return (int)GetValue(NumberRowsOrColumnsProperty); }
    set { SetValue(NumberRowsOrColumnsProperty, value < 1 ? 0 : value); }
}

public static readonly DependencyProperty NumberRowsOrColumnsProperty = DependencyProperty.Register("NumberRowsOrColumns", typeof(int), typeof(PRGD010_GridViewPanel), new PropertyMetadata(1d, OnNumberRowsOrColumnsPropertyChanged));
private static void OnNumberRowsOrColumnsPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
    (source as PRGD010_GridViewPanel).InvalidateMeasure();
}

public int Offset
{
    get { return (int)GetValue(StartPositionProperty); }
    set { SetValue(StartPositionProperty, value >= this.NumberRowsOrColumns ? this.NumberRowsOrColumns - 1 : value); }
}

public static readonly DependencyProperty StartPositionProperty = DependencyProperty.Register("Offset", typeof(int), typeof(PRGD010_GridViewPanel), new PropertyMetadata(0d, OnStartPositionPropertyChanged));
private static void OnStartPositionPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
    (source as PRGD010_GridViewPanel).InvalidateMeasure();
}

public PRGD010_GridViewPanel()
{
}
}

  • XAML:

  • XAML:

    <GridView  ItemsSource="{x:Bind main_viewmodel.prgd010, Mode=OneWay}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <usercontrols:PRGD010_GridViewPanel NumberRowsOrColumns="{x:Bind ViewModel.MyColumns}" Offset="{x:Bind ViewModel.MyOffset" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <GridView.ItemTemplate>
            <DataTemplate x:DataType="classes:PRGD010_Tag">
                <TextBlock Text="{x:Bind ref_cat_id, Mode=OneWay}"/>
            </DataTemplate>
        </GridView.ItemTemplate>
    </GridView>
    

  • 推荐答案

    当你使用 x:Bind 时,绑定的上下文是页面或用户控件本身(而不是普通绑定使用的 DataContext),所以在后面的代码需要 main_viewmodel、View Model 和 ref_cat_id 的属性.如果其中一个或多个为空或不存在,则可以解释您遇到的错误

    When you use x:Bind, the context of the binding is the page or user control itself (rather than the DataContext that the normal binding uses), so in code behind you would need properties for main_viewmodel, View Model, and ref_cat_id. If one or more of those are null or don't exist, that would explain the error that you're getting

    这篇关于UWP c# 绑定到 ItemsPanel/自定义面板(模板)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆