views:

63

answers:

2

Hi Everyone, I have two Data Template (one for drawing[draw] and another for Input Data[data]) Also I have the two ContentControls which uses the above DataTemplates. I want the both DataTemplate's elements to be binded so that when the user fills in a field in the data form DateTemplate it automatically updates the draw Template as well.

How can I bind the elements in draw DataTemplate with the elements of data DataTemplate. There is no backend data at all. User picks up a value from a combobox and based upon the value selected in combobox I update the two ContentControls with relevant draw and data DataTemplates. User fill in the relevant fields in the data form and draw template draws those elements based upon some business Rules.

-----

    <DataTemplate x:Key="data">
        <Grid Grid.Row="0" Background="#FFFFFFFF" Name="DocumentRoot"  VerticalAlignment="Top">
            <Grid.RowDefinitions>
                <RowDefinition Height="auto" />
                <RowDefinition  Height="auto"/>
                <RowDefinition  Height="auto"/>
            </Grid.RowDefinitions>
            <Grid  Margin="10" VerticalAlignment="Top">
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="100" />
                    <ColumnDefinition Width="200" />
                </Grid.ColumnDefinitions>

                <TextBlock Text="Heading Text" Grid.Row="1"/>
                <TextBlock Text="Ticket Text" Grid.Row="2"/>
               -----  
                <TextBox x:Name="txtHeading" Text="Heading Text" Grid.Row="1" Grid.Column="1"/>
                <TextBox x:Name="txtTicketText" Text="Ticket Text"  Grid.Row="2" Grid.Column="1"/>
                -----
            </Grid>


        </Grid>
    </DataTemplate>

 <ContentControl   Content="{Binding ElementName=cboTemplates, Path=SelectedItem.Name}"
                        ContentTemplateSelector="{StaticResource formTemplateSelector}">
                </ContentControl>

Any ideas how can I bind the two elements from inside different DataTemplates?

Thanks in advance

A: 

Why don't you bind one object (of class with a Draw property and a Data property) to both the templates. When one template changes Data property in the object, you can refresh Draw property in the object which in turn will update the Draw template.


Updated


Example :

Window Content

<Grid>
    <StackPanel>
        <ContentControl DataContext="{Binding}">
            <ContentControl.Template>
                <ControlTemplate>
                    <Rectangle Fill="{Binding Background}"
                               Width="200"
                               Height="200" />
                </ControlTemplate>
            </ContentControl.Template>
        </ContentControl>
        <ContentControl DataContext="{Binding}">
            <ContentControl.Template>
                <ControlTemplate>
                    <TextBox Text="{Binding ColorText}" />
                </ControlTemplate>
            </ContentControl.Template>
        </ContentControl>
    </StackPanel>
</Grid>

Code Behind

public partial class MultiViewWindow : Window
{
    public MultiViewWindow()
    {
        InitializeComponent();

        DataContext = new BackgroundInfo();
    }
}

public class BackgroundInfo : INotifyPropertyChanged
{
    protected String _colorText;
    public String ColorText
    {
        get
        {
            return _colorText;
        }
        set
        {
            _colorText = value;
            RaisePropertyChanged("ColorText");
            RaisePropertyChanged("Background");
        }
    }

    public Brush Background
    {
        get
        {
            try
            {
                return new SolidColorBrush((Color)ColorConverter.ConvertFromString(ColorText));
            }
            catch (Exception)
            {
                return new SolidColorBrush(Colors.Transparent);
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    void RaisePropertyChanged(String propertyName)
    {
        PropertyChangedEventHandler temp = PropertyChanged;
        if (temp != null)
        {
            temp(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
decyclone
Could you please give me some sample code?
Jhelumi786
I did try the above code and it did not work.I think its because of the DataContext as the ContentContorl 's Binding is with the comboxBox.
Jhelumi786
What comboBox? And what do you mean by did not work? Did it throw a binding error?
decyclone
Jhelumi786
I think you are getting confused here. I wrote this code in my editor, saw it running, and pasted it here. What you need to do to make this work is create a WPF Application project, add a Window (WPF), paste the <Grid> ... </Grid> code in the Window's content, go to that Window's code behind, paste the C# code there, go to App.xaml, change Startup file name to this Window and run the application. When you enter a color name in textbox, and remove focus, it changes the color in the box above it.
decyclone
i agree that this code does works as its own but in my situation it doesn't. If the ContentControl does bind only with the Windows's DataSource then it works but in my situation the COntentCorntol' binding is with the another control(combobox) so that the DataTemplate can be picked up. Now inside the contentcontrol's DataTemplate i need to bind elements with some other DataContext not the ContentControl's binding.
Jhelumi786
A: 

Consider creating class (named View Model) and bind both templates to single instance of that class (this is Model-View-ViewModel design pattern). Otherwise you probably will have very complex bindings contains hardcoded logical tree.

STO
Any code samples please if possible?
Jhelumi786