繁体   English   中英

将用户控件属性与另一个属性绑定

[英]Binding User Control Property with another Property

在我的Windows Store应用程序中,我想将用户控件中的一个属性与逻辑类中的另一个属性绑定

用户控件“ Card_UC.xaml.cs”包含以下属性:

public string Card_ID
{
    get { return (string)GetValue(Card_ID_Property); }
    set { SetValue(Card_ID_Property, value); }
}

public DependencyProperty Card_ID_Property = 
    DependencyProperty.Register(
        "Card_ID", 
        typeof(string), 
        typeof(Card_UC), 
        new PropertyMetadata(null));

在我的逻辑类“ Card_Data.cs”中:

public string Card_ID { get; set; }

在主页中,我想使用像这样的数据绑定来制作这张卡的网格

<GridView
    x:Name="UI_GView_Cards"
    ItemsSource="{Binding}">
    <GridView.ItemTemplate>
        <DataTemplate>
            <local:CardControl
                x:Name="UC_Card"
                CardPressed="CardControl_CardPressed"
                ID="{Binding Path=Card_ID, ElementName=Card_UC, Mode=TwoWay}"/>
        </DataTemplate>
    </GridView.ItemTemplate>
</GridView>

Card_ID外,“ Card_UC.xaml”中所有其他属性的绑定均有效

现在的问题是,每次使用以下命令访问ID属性时,应用程序都将崩溃

return (string)GetValue(Card_ID_Property);

错误:“对象引用未设置为对象的实例。”


解决的问题:

此行中的问题:

ID="{Binding Path=Card_ID, ElementName=Card_UC, Mode=TwoWay}"

变成:

ID="{Binding Card_ID}"

编辑:

  • 修复了“复制/粘贴”错误。
  • 重新格式化问题。

看来您有剪切和粘贴错误:

public string Card_ID
{
   get { return (string)GetValue(UCAppsProperty); }
   set { SetValue(UCAppsProperty, value); }
}

至:

public string Card_ID
{
   get { return (string)GetValue(Card_ID_Property); }
   set { SetValue(Card_ID_Property, value); }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM