This is a migrated thread and some comments may be shown as answers.

[Solved] Column Binding to UserControl

4 Answers 103 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Alex
Top achievements
Rank 1
Alex asked on 11 Oct 2010, 01:39 PM
Hi, 
I have a User Control unde Silverlight
Something simple
XAML
<UserControl x:Class="RepaymentSystem.GridTextBlockCell"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="28" d:DesignWidth="65">

 <Grid x:Name="LayoutRoot" Background="Transparent">
<StackPanel HorizontalAlignment="Right" VerticalAlignment="Top" >
            <TextBlock x:Name="txtblCell" Text="300"  Margin="0,2,22,0" HorizontalAlignment="Right"/>
            <TextBlock Text="₪" Margin="0,-17,49,0" HorizontalAlignment="Right"/>
            <TextBlock Text="העתק" Margin="0,-5,20,0" Foreground="Blue" HorizontalAlignment="Right"/>
        </StackPanel>
    </Grid>
<UserControl/>

and the CS


 public partial class GridTextBlockCell : UserControl
    {
        public GridTextBlockCell()
        {
            InitializeComponent();
            LayoutRoot.DataContext = ViewModel.m_AppDataSource;
            txtblCell.Text = TextBlockDetailsText;
        }
        
        private string m_TextBlockDetailsText = "";

        public string TextBlockDetailsText
        {
            get { return m_TextBlockDetailsText; }
            set { m_TextBlockDetailsText = value; txtblCell.Text = value; }
        }
    }


What I am trying to do is Use this user control in a Telerik GridView Column to display some data.

<telerik:GridViewDataColumn Width="70"  Header="חיוב החודש" DataMemberBinding="{Binding BillingMonth}" TextAlignment="Center" HeaderTextAlignment="Right" IsReadOnly="True">

             <telerik:GridViewDataColumn.CellTemplate >

             <DataTemplate>
             <!--UserControlCell-->
             <local:GridTextBlockCell  x:Name="aaa"TextBlockDetailsText="aaa"></local:GridTextBlockCell>
            
             </DataTemplate>
             </telerik:GridViewDataColumn.CellTemplate>
             </telerik:GridViewDataColumn>
How it looks right now works fine (static "aaa" text)
But if I try to bind TextBlockDetailsText="{Binding BillingMonth}"  it get's an error.


Webpage error details

User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; InfoPath.2; .NET4.0C; .NET4.0E)
Timestamp: Mon, 11 Oct 2010 12:38:45 UTC


Message: Unhandled Error in Silverlight Application Element is already the child of another element. [Line: 0 Position: 0]
Line: 1
Char: 1
Code: 0
URI: http://localhost/Repayment/RepaymentSystemTestPage.aspx

I'm assuming I'm doing something wrong but I don't know what.

I would appreciate your help.

Thanks a lot,
Alex

4 Answers, 1 is accepted

Sort by
0
Accepted
Veselin Vasilev
Telerik team
answered on 11 Oct 2010, 02:33 PM
Hi Alex,

You need to define TextBlockDetailsText as a dependency property in order to bind to it.

public string MyTextBlockDetailsText
{
    get { return (string) GetValue(MyTextBlockDetailsTextProperty); }
    set { SetValue(MyTextBlockDetailsTextProperty, value); }
}
// Using a DependencyProperty as the backing store for MyTextBlockDetailsText.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty MyTextBlockDetailsTextProperty =
    DependencyProperty.Register("MyTextBlockDetailsText", typeof(string), typeof(SilverlightControl1), new PropertyMetadata(""));

Also, you can bind the Text of the TextBlock to this dependency property:

<TextBlock x:Name="txtblCell" Margin="0,2,22,0" HorizontalAlignment="Right" Text="{Binding MyTextBlockDetailsText}"/>

Hope this helps.

Greetings,
Veselin Vasilev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Alex
Top achievements
Rank 1
answered on 11 Oct 2010, 03:27 PM
Hi Vaselin,
First of all thanks for your prompt answer. Indeed this seams to be the solution  but it's not working for me too.

Now my files look like this
XAML
<TextBlock x:Name="txtblCell" Text="{Binding TextBlockDetailsText}"  Margin="0,2,22,0" HorizontalAlignment="Right"/><br>

CS
public partial class GridTextBlockCell : UserControl
    {
        public GridTextBlockCell()
        {
            InitializeComponent();
            LayoutRoot.DataContext = ViewModel.m_AppDataSource;
             
        }
 
        public string TextBlockDetailsText
        {
            get { return (string)GetValue(TextBlockDetailsTextProperty); }
            set { SetValue(TextBlockDetailsTextProperty, value); }
        }
    
        public static readonly DependencyProperty TextBlockDetailsTextProperty =
        DependencyProperty.Register("TextBlockDetailsText", typeof(string), typeof(GridTextBlockCell), new PropertyMetadata(""));


However the code never hits GET or SET from TextBlockDetailsText(I placed a breakpoint there) if I bind the control to data source.
TextBlockDetailsText="{Binding BillingMonth}"

If i set it staticly
TextBlockDetailsText="aaa"
hits SET but the changes don't show in the grid

Any other Ideas?



Thanks a lot,
Alex
0
Accepted
Veselin Vasilev
Telerik team
answered on 11 Oct 2010, 03:30 PM
Hi Alex,

Try:
public GridTextBlockCell()
        {
            InitializeComponent();
            LayoutRoot.DataContext = this;
               
        }



Sincerely yours,
Veselin Vasilev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Alex
Top achievements
Rank 1
answered on 11 Oct 2010, 03:45 PM
Hi Vaselin,

It worked!
Your a real life saver.


Thanks a lot,
Alex
Tags
GridView
Asked by
Alex
Top achievements
Rank 1
Answers by
Veselin Vasilev
Telerik team
Alex
Top achievements
Rank 1
Share this question
or