簡體   English   中英

WPF綁定到單個控件(不是列表)

[英]WPF binding to single control (not a list)

簡而言之,我正在嘗試創建一個DataTemplate來指示客戶橫幅的外觀。

我已經以非常簡單的形式進行了此工作,但是僅使用了ListView控件,將ItemsSource應用於該控件,該列表包含一個條目。

我想做的是將Customer對象直接應用於控件(不確定控件的類型),然后為該類型選擇DataTemplate並布置數據。

我正在使用的xaml是...

<Window x:Class="WpfApplication5.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication5"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <DataTemplate DataType="{x:Type local:Customer}" >
            <Border Background="Blue" >
                <TextBlock Text="{Binding CustomerName}"  />
            </Border>
        </DataTemplate>
    </Window.Resources>
    <ListView x:Name="mylist" />
</Window>

帶有以下代碼。

namespace WpfApplication5
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Customer mp=new Customer();
            mp.CustomerName="Mr. Banana";
            List<Customer> temp = new List<Customer>();
            temp.Add(mp);
            mylist.ItemsSource = temp;
        }
    }
    public class Customer
    {
        public string CustomerName { get; set; }
    }
}

只需使用ContentControl

<ContentControl x:Name="banner" />

並在代碼中:

banner.Content = mp;

創建自己的UserControl ,其中將包含可用於綁定的Customer屬性。 然后,您可以使用RelativeSource綁定綁定到該屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM