S
S
ShpriZZ2022-01-11 16:27:00
WPF
ShpriZZ, 2022-01-11 16:27:00

Is there an analogue of the "ItemsControl" element, but for a single element?

Let's say I have a phone class with its cost:

public class Phone 
{
    public int Price { get; set; }

    pulic Phone(int price) 
    { 
        Price = price; 
    }
}

And two heir classes:
public class Xiaomi : Phone { public Xiaomi(int price) : base(price) { }}
public class IPhone : Phone { public IPhone(int price) : base(price) { }}

If I wanted to display a collection of phones, I would create an ObservableCollection in the window's ViewModel:
public ObservableCollection<Phone> Phones{ get => _Phones; set => _Phones= value; }
private ObservableCollection<Phone> _Phones = new();

And on the form I would define the ItemsControl element:
<ItemsControl ItemsSource="{Binding Phones}">
    <ItemsControl.Resources>
        <DataTemplate DataType="{x:Type Xiaomi}">
            <TextBlock Text="Этот телефон Xiaomi стоит:"/>
            <TextBlock Text="{Binding Path=Price}"/>
        </DataTemplate>

         <DataTemplate DataType="{x:Type IPhone}">
            <TextBlock Text="Этот телефон IPhone стоит:"/>
            <TextBlock Text="{Binding Path=Price}"/>
        </DataTemplate>
    </ItemsControl.Resources>
</ItemsControl>


So I could display a collection of phones in the window, but how do I display one phone?
Let's say in my code I have:
public Phone Telephone { get => _Telephone; set => _Telephone; }
private Phone _Telephone = new Xiaomi();

Is there some element so that I can output this to the form? Using an ItemsControl and creating a collection for a single element looks very crutch.

Someone said that you can use it for this ContentControl, but they didn’t explain exactly how, and there is no information on the Internet.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
ShpriZZ, 2022-01-11
@ShpriZZ

So, I found a normal solution. No crutches, no inefficient code. I knew there was an elegant solution. I'll post the old solution in the comments, because it's a pity the time wasted writing it.
To display a class object from the ViewModel, depending on its type, the element is used ContentPresenter. In xaml its usage looks something like this:

<ContentPresenter Content="{Binding Telephone}">
        <ContentPresenter.Resources>
        <DataTemplate DataType="{x:Type cl:Xiaomi}">
            <Label Content="Это ксяоми!"/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type cl:IPhone}">
            <Label Content="Ну а это не очень телефон такой..."/>
        </DataTemplate>
    </ContentPresenter.Resources>
</ContentPresenter>

Ps I was told about this element, but for some reason I thought about ElementControl, I don’t even know why.

V
Vladimir Korotenko, 2022-01-11
@firedragon

A collection of one element will display one element.
However, you can just create a separate variable
https://metanit.com/sharp/wpf/11.php

<StackPanel>
       <TextBlock Text="Этот телефон Xiaomi стоит:"/>
       <TextBlock Text="{Binding XiomiPrice}"/>
</StackPanel>

And yes, for understanding
https://docs.microsoft.com/ru-ru/dotnet/desktop/wp...
Any element with an ItemsSource loops through your collection, passing a single element to your template, and inside the template you bind to data along the way

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question