locked
Multiple DataBinding RRS feed

  • Question

  • Hello,

    I am relatively new in Silverlight applications, I have different UserControls and I want to apply multiple databinding on them.

    For example, I want to apply databinding on the Text of a TextBox and its foreground also. That means that I have different DataContext!

    Is it possible to resolve this problem?

    Thanks a lot,

    Hela

    Saturday, February 7, 2009 6:14 AM

Answers

  • Hi,

    Do you mean this?

    xaml.cs:

    namespace SilverlightApplication1

    {

    public partial class Page : UserControl

    {

    public Page()

    {

     

    InitializeComponent();

    }

     

    }

    public class MyClass

    {

    public string Text { get; set; }public Brush Brush { get; set; }

    }

    }

    xaml:

    <UserControl x:Class="SilverlightApplication1.Page"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:c="clr-namespace:SilverlightApplication1"

    Width="400" Height="300">

    <UserControl.Resources>

    <c:MyClass x:Key="datasource" Text="Hello World!">

    <c:MyClass.Brush>

    <SolidColorBrush Color="Red"></SolidColorBrush>

    </c:MyClass.Brush>

    </c:MyClass>

    </UserControl.Resources>

    <Grid x:Name="LayoutRoot" Background="White">

    <TextBlock DataContext="{StaticResource datasource}" x:Name="TextBlock1" Text="{Binding Text}" Foreground="{Binding Brush}"></TextBlock>

    </Grid>

    </UserControl>

    Tuesday, February 10, 2009 5:16 AM

All replies

  • Hello,

    I re-explain my need:I want to apply databinding on the Text of a TextBox and its foreground also at the same time. I have a class that contains the colors for binding the foreground and a Datagrid from where I bound the value of the Text property of the TextBox.

    Please I need help.

    Thanks a lot,

    Hela

    Monday, February 9, 2009 5:37 AM
  • Hi,

    Do you mean this?

    xaml.cs:

    namespace SilverlightApplication1

    {

    public partial class Page : UserControl

    {

    public Page()

    {

     

    InitializeComponent();

    }

     

    }

    public class MyClass

    {

    public string Text { get; set; }public Brush Brush { get; set; }

    }

    }

    xaml:

    <UserControl x:Class="SilverlightApplication1.Page"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:c="clr-namespace:SilverlightApplication1"

    Width="400" Height="300">

    <UserControl.Resources>

    <c:MyClass x:Key="datasource" Text="Hello World!">

    <c:MyClass.Brush>

    <SolidColorBrush Color="Red"></SolidColorBrush>

    </c:MyClass.Brush>

    </c:MyClass>

    </UserControl.Resources>

    <Grid x:Name="LayoutRoot" Background="White">

    <TextBlock DataContext="{StaticResource datasource}" x:Name="TextBlock1" Text="{Binding Text}" Foreground="{Binding Brush}"></TextBlock>

    </Grid>

    </UserControl>

    Tuesday, February 10, 2009 5:16 AM
  • Hi Allen,

    thnx a lot, it was really helpful for me.

    Monday, February 16, 2009 7:40 AM
  • Wht not using a style since you define constants in Resources

    <Style x:Key="RedBox" TargetType="TextBox">
        <Setter Property="Text" Value="Hello World!" />
        <Setter Property="Foreground" Value="Red" />
    </Style>
    And using it like this:
    <TextBox Style="{StaticResource RedBox}" />
     
    Thursday, February 19, 2009 5:12 AM