0

This is a problem I've been trying to solve for a couple of days but I'm yet to find a good solution.

I have a main WPF window which contains a StackPanel. I also have a class called "MessageManagement" which contains a List of custom WPF controls called "MessagePreview".

MessagePreview

<UserControl x:Class="FinalYearProject.MessagePreview"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="58" d:DesignWidth="254" Background="White" BorderThickness="1" BorderBrush="#FF320000">
<Grid Name="grid1">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="50" MaxWidth="50" MinWidth="50" />
        <ColumnDefinition Width="197*" MinWidth="20" />
    </Grid.ColumnDefinitions>
    <Grid Grid.Column="1" Name="grid2">
        <Grid.RowDefinitions>
            <RowDefinition Height="31*" />
            <RowDefinition Height="34*" />
            <RowDefinition Height="33*" />
        </Grid.RowDefinitions>
        <Label Content="-ERROR-" Height="28" HorizontalAlignment="Left" Margin="2,1,0,0" Name="senderLabel" VerticalAlignment="Top" Foreground="#FF0000B4" Grid.RowSpan="2" />
        <Label Content="-ERROR-" Height="33" HorizontalAlignment="Left" Name="previewLabel" VerticalAlignment="Top" Margin="-1,17,0,0" Grid.RowSpan="3" />
        <Label Content="-Error-" Grid.Row="1" Height="28" HorizontalAlignment="Left" Margin="0,16,6,0" Name="timeLabel" VerticalAlignment="Top" Grid.RowSpan="2" Width="196" />
    </Grid>
    <Grid Name="grid3">
        <Grid.RowDefinitions>
            <RowDefinition Height="29*" />
            <RowDefinition Height="27*" />
        </Grid.RowDefinitions>
        <Image Height="38" HorizontalAlignment="Center" Margin="3,0,6,20" Name="image1" Stretch="Fill" VerticalAlignment="Bottom" Width="41" Source="/FinalYearProject;component/Images/twitter-bird-light-bgs.png" Grid.RowSpan="2" />
        <CheckBox Height="16" Margin="15,0,22,6" Name="checkBox" VerticalAlignment="Bottom" Grid.Row="1" IsChecked="False" />
    </Grid>
</Grid>

MessageManagement

List<MessagePreview> unread = new List<MessagePreview>();

    public bool messagesLocked()
    {
        foreach (MessagePreview m in unread)
        {
            if ((bool)m.checkBox.IsChecked)
                return true;
        }
        return false;
    }

MainWindow C#

MessageManagement messageManagement;

MessagesPanel1 is a blank StackPanel inside MainWindow

if (!messageManagement.messagesLocked())
{
       foreach (Message m in messageManagement.getListOfMessages()
       {
              MessagesPanel1.Children.Add(m)
       }
}

When you add the custom control to the StackPanel it creates a duplicate of the original, which means changing a value on the controls in the StackPanel does not affect the controls in my MessageManagement List.

Is there a way that I can reference my original controls so that they change together? I looked into DataBinding but I'm not sure if that's the right area.

Algirdas
  • 677
  • 1
  • 6
  • 15
  • 2
    what?! post your xaml and code. Also if you need a control that shows several items that would be an `ItemsControl` or one of its derivatives (such as `ListBox`) – Federico Berasategui Jan 16 '13 at 16:14
  • Are you using two way or one way binding in your Message control? I don't think that your list of controls contains copys of controls, because UserControl is a reference type... – acrilige Jan 16 '13 at 16:15
  • This sounds like job for the DataBinding. Instead of having a manager contain list of custom controls, have it only hold the data - for example the message text. Then use some sort of repeater control (for example ListBox) and create an ItemTemplate so that it uses your Message control for each item, and bind that repeater to your messages collection. This is not a trivial task, but with some sort of tutorial (or a good book like WPF 4 Unleashed) it's not that hard. – Honza Brestan Jan 16 '13 at 16:18
  • I made the changes to my post to include code, hopefully that makes the problem a bit clearer. – Mark Eastwood Jan 16 '13 at 16:36

2 Answers2

1

Again, You do not make a List of UI elements in WPF. You use an ItemsControl and set its ItemTemplate Property to whatever UI you need to represent your items:

 <ItemsControl ItemsSource="{Binding Messages}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <!-- Everything you placed in your usercontrol, or else an instance of your usercontrol itself -->
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154
0

If saying "change" you mead a data, inside WPF you never and ever need to access UI elements directly, as they are subject to change. What you have to do is to read the data bound to them. With correct databinding setupped on that controls you will have always updated information.

Accessing UI elements is never easy, but except that, it's completely unreliable way to access your data, as the way control is injected into the VisualTree may be changed, so your code will fail on next update.

For example on how to architect that can have a look here:

WPF Data Binding Examples

Tigran
  • 61,654
  • 8
  • 86
  • 123