M
M
Mikhail Usotsky2018-11-08 14:32:50
WPF
Mikhail Usotsky, 2018-11-08 14:32:50

How to properly implement binding between elements and arrays?

There is a data template:

<DataTemplate x:Key="ControlChannelDevice">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="34"/>
            <RowDefinition Height="34"/>
            <RowDefinition Height="34"/>
            <RowDefinition Height="34"/>
            <RowDefinition Height="34"/>
            <RowDefinition Height="34"/>
            <RowDefinition Height="34"/>
            <RowDefinition Height="34"/>
        </Grid.RowDefinitions>

        <Label Grid.Column="0" Grid.Row="0" Content="Text_1" HorizontalAlignment="Center"/>
        <Label Grid.Column="1" Grid.Row="0" Content="Text_2" HorizontalAlignment="Center"/>

        <Button Grid.Column="0" Grid.Row="1" Content="Command_1" Command="{Binding Command_1}"/>
        <Button Grid.Column="1" Grid.Row="1" Content="Command_2" Command="{Binding Command_2}"/>

        <ToggleButton Grid.Column="0" Grid.Row="2" Content="Status_1" IsChecked="{Binding Status_1}"/>
        <ToggleButton Grid.Column="1" Grid.Row="2" Content="Status_2" IsChecked="{Binding Status_2}"/>

        <ToggleButton Grid.Column="0" Grid.Row="3" Content="Status_3" IsChecked="{Binding Status_3}"/>
        <ToggleButton Grid.Column="1" Grid.Row="3" Content="Status_4" IsChecked="{Binding Status_4}"/>

        <Label Grid.Column="0" Grid.Row="4" Content="Select_1"/>
        <ComboBox Grid.Column="1" Grid.Row="4" SelectedIndex="{Binding Select_1}"/>

        <Label Grid.Column="0" Grid.Row="5" Content="Select_2"/>
        <ComboBox Grid.Column="1" Grid.Row="5" SelectedIndex="{Binding Select_2}"/>

        <Label Grid.Column="0" Grid.Row="6" Content="Select_3"/>
        <ComboBox Grid.Column="1" Grid.Row="6" SelectedIndex="{Binding Select_3}"/>

        <Label Grid.Column="0" Grid.Row="7" Content="Select_4"/>
        <ComboBox Grid.Column="1" Grid.Row="7" SelectedIndex="{Binding Select_4}"/>

    </Grid>
</DataTemplate>

And there is a class with ViewModel - ControlDevice.
Wrote code for event handling.
public class ControlChannelsDevice : BaseViewModel
{
    public ControlChannelsDevice()
    {
        foreach (ControlDevice elements in ControlChannelDevice)
            elements.PropertyChanged += Elements_PropertyChanged;
    }

    private void Elements_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        RaisePropertyChanged();
    }

    ControlDevice[] _controlChannelDevice = new ControlDevice[4]
    {
        new ControlDevice(),
        new ControlDevice(),
        new ControlDevice(),
        new ControlDevice()
    };
    public ControlDevice[] ControlChannelDevice => _controlChannelDevice;
    
}

I create elements:
<ContentControl Grid.Row="1" Name="Channel_1" ContentTemplate="{StaticResource ControlChannelDevice}"/>
<ContentControl Grid.Row="1" Name="Channel_2" ContentTemplate="{StaticResource ControlChannelDevice}"/>
<ContentControl Grid.Row="1" Name="Channel_3" ContentTemplate="{StaticResource ControlChannelDevice}"/>
<ContentControl Grid.Row="1" Name="Channel_4" ContentTemplate="{StaticResource ControlChannelDevice}"/>

Also I do bindings:
ControlChannelsDevice ControlChannelsDevice { get; set; } = new ControlChannelsDevice();
Channel_1.DataContext = ControlChannelsDevice.ControlChannelDevice[0];
Channel_2.DataContext = ControlChannelsDevice.ControlChannelDevice[1];
Channel_3.DataContext = ControlChannelsDevice.ControlChannelDevice[2];
Channel_4.DataContext = ControlChannelsDevice.ControlChannelDevice[3];
And signed up for the whole event. But for some reason the events are not working.
If you just create one element without an array and bind it, it works fine. Initially, in the data template, instead of a key, there was DataType for ControlDevice.
I tried to put UserControl instead of data template, and Content instead of ContentTemplate. There are events, but for some reason the data is the same for all four ContentControl. When switching to the next ContentControl, the previous one is no longer restored (disappears).
With arrays elsewhere in the program, this works fine, but they work directly with properties. And this approach did not work on the current one.
How to solve the connection problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Usotsky, 2018-11-08
@AquariusStar

Problem solved.
I had to fix:

<ContentControl Grid.Row="1" Name="Channel_1" Content="{Binding ControlChannelDevice[0]}"/>
<ContentControl Grid.Row="1" Name="Channel_2" Content="{Binding ControlChannelDevice[1]}"/>
<ContentControl Grid.Row="1" Name="Channel_3" Content="{Binding ControlChannelDevice[2]}"/>
<ContentControl Grid.Row="1" Name="Channel_4" Content="{Binding ControlChannelDevice[3]}"/>

and:
Channel_1.DataContext = ControlChannelsDevice;
Channel_2.DataContext = ControlChannelsDevice;
Channel_3.DataContext = ControlChannelsDevice;
Channel_4.DataContext = ControlChannelsDevice;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question