M
M
mississippi2018-12-26 10:34:03
WPF
mississippi, 2018-12-26 10:34:03

How to bind data of different types to one control and output depending on a certain condition?

Now, if I have a window in which I want to display, say, numeric data, while the control and data source are bound (Binding), and under certain conditions I need to display a string in the same place. How to do it? Make two bindings to the same element? Or make a separate element for each data source and display it under a certain condition?
I write the interface in XAML. I am using mvm.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
T
tex0, 2018-12-26
@tex0

DataTemplateSelector
will help you some more examples

A
Alexey Pavlov, 2018-12-26
@lexxpavlov

You can make DataTemplate templates that will change the template of the desired type:

<Window x:Class="TestApp.MainWindow"
        xmlns:testApp="clr-namespace:TestApp"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <DataTemplate DataType="{x:Type testApp:TestInt}">
            <TextBlock Text="{Binding Value}" Background="Red"/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type testApp:TestStr}">
            <TextBlock Text="{Binding Value}" Background="Yellow"/>
        </DataTemplate>
    </Window.Resources>
    <StackPanel>
            <ContentControl Content="{Binding Test1}"/>
            <ContentControl Content="{Binding Test2}"/>
            <ListBox ItemsSource="{Binding TestList}"/>
    </StackPanel>
</Window>

public class TestInt
{
    public int Value { get; set; }
}
public class TestStr
{
    public string Value { get; set; }
}
public partial class MainWindow : Window
{
    public TestInt Test1 { get; set; }
    public TestStr Test2 { get; set; }
    public List<object> TestList { get; set; } 

    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
        Test1 = new TestInt { Value = 123 };
        Test2 = new TestStr { Value = "asd" };
        TestList = new List<object> { Test1, Test2 };
    }

In order for data templates to be automatically included, the type must be specified via DataType="{x:Type testApp:TestInt}", and not via DataType="testApp:TestInt"

F
Foggy Finder, 2018-12-28
@FoggyFinder

Maybe data triggers are for you .
For example, you want to display a message saying there is no data to display if the collection of numeric data is empty:

<ListBox ItemsSource="{Binding CurrentData}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding .}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
    <ListBox.Style>
        <Style TargetType="ListBox">
            <Style.Triggers>
                <Trigger Property="HasItems" Value="False">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <TextBlock
                                    Margin="5"
                                    HorizontalAlignment="Center"
                                    VerticalAlignment="Center"
                                    FontSize="16"
                                    Text="{Binding StringIfEmpty}" />
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListBox.Style>
</ListBox>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question