簡體   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