第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

wpf將不同的數(shù)據(jù)模板綁定到contentcontrol中不同類型的對(duì)象

wpf將不同的數(shù)據(jù)模板綁定到contentcontrol中不同類型的對(duì)象

C#
守候你守候我 2022-01-15 17:21:57
我是 WPF 和 MVVM 的新手。我要做的是將兩個(gè)不同的 DataTemplates 綁定到一個(gè) ContentControl 中的兩種不同類型的對(duì)象。每種對(duì)象對(duì)應(yīng)一個(gè)DataTemplate。這兩種對(duì)象分別稱為單元和組件。它們包含不同的屬性。例如,一個(gè) Unit 有 3 個(gè)屬性:Id、Name和Manufacture。一個(gè)組件有 3 個(gè)屬性Id、Type和Materials。示例代碼如下:public class Unit : INotifyPropertyChanged{    private int _id;    private string _name;    private string _manufacture;    public int Id    {        get {return this._id}        set        {            this._id = value;            OnPropertyChanged("Id")        }    {    public string Name    {        get {return this._name}        set        {            this._id = value;            OnPropertyChanged("Name")        }    {    public string Manufacture    {        get {return this._manufacture}        set        {            this._id = value;            OnPropertyChanged("Manufacture")        }    {    public event PropertyChangedEventHandler PropertyChanged;    ...}Component 類具有類似的結(jié)構(gòu)。在 MainWindow 中,左側(cè)有一個(gè) ListBox 列出對(duì)象的名稱(我以后會(huì)將其更改為 TreeView),右側(cè)有一個(gè) ContentControl。我希望當(dāng)我選擇一個(gè)對(duì)象的名稱時(shí),該對(duì)象的詳細(xì)信息將顯示在右側(cè)。MainWindow的代碼如下:<Windows.Resources>    <CollectionViewSource        Source="{Binding Source={x:Static Application.Current}, Path=UnitItems}"        x:Key="UnitDataView">    </CollectionViewSource>    <CollectionViewSource        Source="{Binding Source={x:Static Application.Current}, Path=ComponentItems}"        x:Key="ComponentDataView">    </CollectionViewSource>    <CompositeCollection x:Key="AllDataView        <CollectionContainer Collection="{Binding Source={StaticResource UnitDataView}}" />        <CollectionContainer Collection="{Binding Source={StaticResource ComponentDataView}}" />    </CompositeCollection><local: PartDataTemplateSelector x:Key="MyDataTemplateSelector"                                 UnitTemplate="{StaticResource unitTemplate}"                                 ComponentTemplate="{StaticResource componentTemplate}" /></Windows.Resources>
查看完整描述

1 回答

?
慕尼黑的夜晚無繁華

TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超6個(gè)贊

您不需要 DataTemplateSelector。只需確??梢宰詣?dòng)選擇詳細(xì)的 DataTemplates,而不是為其分配密鑰。


您的對(duì)象似乎也不需要兩個(gè)集合。您不妨從一個(gè)公共基類派生 Unit 和 Component,并擁有一個(gè)基類引用集合。


最后應(yīng)該有一個(gè)視圖模型,除了對(duì)象集合之外,它還具有當(dāng)前選定對(duì)象的屬性。


以這個(gè)簡化的示例視圖模型為例:


public class Base

{

    public int Id { get; set; }

}


public class Unit : Base

{

    public string UnitData { get; set; }

}


public class Component : Base

{

    public string ComponentData { get; set; }

}


public class ViewModel : INotifyPropertyChanged

{

    public event PropertyChangedEventHandler PropertyChanged;


    public ObservableCollection<Base> Objects { get; }

        = new ObservableCollection<Base>();


    private Base selectedObject;


    public Base SelectedObject

    {

        get { return selectedObject; }

        set

        {

            selectedObject = value;

            PropertyChanged?.Invoke(this,

               new PropertyChangedEventArgs(nameof(SelectedObject)));

        }

    }

}

它的一個(gè)實(shí)例應(yīng)該分配給窗口的 DataContext:


public MainWindow()

{

    InitializeComponent();


    var vm = new ViewModel();

    vm.Objects.Add(new Unit { Id = 1, UnitData = "Unit Data" });

    vm.Objects.Add(new Component { Id = 2, ComponentData = "Component Data" });


    DataContext = vm;

}

最后,XAML 將是這樣的:


<ListBox ItemsSource="{Binding Objects}"

            SelectedItem="{Binding SelectedObject}">

    <ListBox.Resources>

        <DataTemplate DataType="{x:Type local:Unit}">

            <TextBlock>

                <Run Text="Unit, Id:"/>

                <Run  Text="{Binding Id}"/>

            </TextBlock>

        </DataTemplate>

        <DataTemplate DataType="{x:Type local:Component}">

            <TextBlock>

                <Run Text="Component, Id:"/>

                <Run  Text="{Binding Id}"/>

            </TextBlock>

        </DataTemplate>

    </ListBox.Resources>

</ListBox>

<ContentControl Grid.Column="1" Content="{Binding SelectedObject}">

    <ContentControl.Resources>

        <DataTemplate DataType="{x:Type local:Unit}">

            <StackPanel>

                <TextBlock Text="{Binding Id}"/>

                <TextBlock Text="{Binding UnitData}"/>

            </StackPanel>

        </DataTemplate>

        <DataTemplate DataType="{x:Type local:Component}">

            <StackPanel>

                <TextBlock Text="{Binding Id}"/>

                <TextBlock Text="{Binding ComponentData}"/>

            </StackPanel>

        </DataTemplate>

    </ContentControl.Resources>

</ContentControl>


查看完整回答
反對(duì) 回復(fù) 2022-01-15
  • 1 回答
  • 0 關(guān)注
  • 274 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)