开发者

Silverlight how to bind my usercontrol in Data Grid column template

开发者 https://www.devze.com 2023-03-01 18:35 出处:网络
today I getting crazy while trying to do, what I think, is simple thing. I want to be able to create my usercontrol, and use it in my column template in my datagrid

today I getting crazy while trying to do, what I think, is simple thing.

I want to be able to create my usercontrol, and use it in my column template in my datagrid

I have searched and tried several combinations, and nothing appear to work

Can anyone help me?

public class User
{
    public string Name { get; set; }
    public bool IsValid { get; set; }
}

partial class MyControl : UserControl
{
    private string _value;
    public string Value
    {
        get { return _value; }
        set { _value = value;
            txt.Text = value;
        }
    }
    public MyControl()
    {
        InitializeComponent();
    }
}

<Grid x:Name="LayoutRoot" Background="White">
    <TextBlock x:Name="txt" Text="[undefined]"></TextBlock>
</Grid>

public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();

        var items = new List<User>();
        items.Add(new User{Name = "user 1", IsValid = true});
        items.Add(new User { Name = "user 2", IsValid = false });

        myGrid.ItemsSource = items;

    }
}

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

    <sdk:DataGrid x:Name="myGrid" AutoGenerateColumns="False" IsReadOnly="True">
        <sdk:DataGrid.Columns>
            <sdk:DataGridTemplateColumn Header="Name">
                <sdk:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <SilverlightApplication1:MyControl Value="{Binding Name}"></SilverlightApplication1:MyControl>
                        <!--<TextBlock Text="{Binding Name}"></TextBlock>-->
                    </DataTemplate>
                </sdk:DataGridTemplateColumn.CellTemplate>
            </sdk:DataGridTemplateColumn>

        </sdk:DataGrid.Columns>
    </sdk:DataGrid>

</Grid>

Edited: I also tried the following, but I get no results on my grid:

<Grid x:Name="LayoutRoot" Background="White">
    <TextBlock x:Name="txt" Text="{Binding Value}"></TextBlock>
</Grid>

   开发者_JS百科public partial class MyControl : UserControl
    {
    public DependencyProperty ValueProperty =
        DependencyProperty.Register("Value",
        typeof(string),
        typeof(MyControl),
         new PropertyMetadata(OnValueChanged));


    public string Value
    {
        get
        {
            return (string)GetValue(ValueProperty);
        }
        set
        {
            SetValue(ValueProperty, value);
            NotifyPropertyChanged("Value");
        }
    }

    private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ((MyControl) d).Value = (String)e.NewValue; //ERROR: here I got always empty string
    }

    public MyControl()
    {
        InitializeComponent();
    }
}


The reason why your first code didn't work is simple. To be able to bind the "Value" property on your "MyControl" (Value={Binding Name}), it has to be a Dependency Property. which you fixed in your second bit of code.

Here's what I did (and that worked well):

<UserControl x:Class="BusinessApplication8_SOF_Sandbox.Controls.MyControl"
             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="300" d:DesignWidth="400" Name="myControl">
    <Grid x:Name="LayoutRoot" Background="White">
        <TextBlock Name="textBlock" Text="{Binding Value, ElementName=myControl}"/>
    </Grid>
</UserControl>

For the rest, I used your code.

Another possibility, which should be OK in case you only want the data to flow in one direction ("One Way" from source to target), as it is the case when using the TextBlock control is to update the Text property in the "OnValueChanged". here's the code for the Value property:

public static readonly DependencyProperty ValueProperty =
        DependencyProperty.Register("Value", typeof(string), typeof(MyControl),
            new PropertyMetadata("", OnValueChanged));
public string Value
{
   get { return (string)GetValue(ValueProperty); }
   set { SetValue(ValueProperty, value); }
}
private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
   var target = (MyControl)d;
   var oldValue = (string)e.OldValue;
   var newValue = target.Value;
   target.OnValueChanged(oldValue, newValue);
}
protected virtual void OnValueChanged(string oldValue, string newValue)
{
    textBlock.Text = newValue;
} 

and you can remove the binding in xaml:

<TextBlock Name="textBlock" />

this worked for me as well.

Hope this helps ;)


you need to implement the INotifyPropertyChanged interface in your User class so that bound user controls are aware if any of the bound properties change. See the following page with the details how to implement it : http://www.silverlightshow.net/tips/How-to-implement-INotifyPropertyChanged-interface.aspx

As you can see you need to implement the interface and in the setters raise the event OnPropertyChanged

Then it should work with your bindings.

Best, Tim

0

精彩评论

暂无评论...
验证码 换一张
取 消