Home > Blockchain >  Use Custom Control binding in DataTemplate
Use Custom Control binding in DataTemplate

Time:04-11

I just want make some of my custom control for the render performance. And Bind my Data

making custom control is OK, but i can't set dependancyProperty for data bind.

I made a Custom Control File

CustomCotrol1.cs

    public class CustomControl1 : Control
    {
        static CustomControl1()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl1), new FrameworkPropertyMetadata(typeof(CustomControl1)));
        }


        public string MyProperty
        {
            get { return (string)GetValue(MyPropertyProperty); }
            set { SetValue(MyPropertyProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MyPropertyProperty =
            DependencyProperty.Register("MyProperty", typeof(string), typeof(CustomControl1), new PropertyMetadata(0));


    }

And set my Generic.xml

    <Style TargetType="{x:Type views:CustomControl1}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type views:CustomControl1}">
                    <Button
                        Width="50"
                        Height="50"
                        Content="{TemplateBinding MyProperty}"
                        >
                        
                    </Button>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

And use it in my view

        <ListBox x:Name="TestListBox" Grid.Row="0"
                 ItemsSource="{Binding DataList}"
                 >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>
                        <RichTextBox  
                            Grid.Row="0"
                            TextChanged="rtb_TextChanged" 
                            HorizontalAlignment="Left"
                            >
                            <RichTextBox.Resources>
                                <Style TargetType="{x:Type Paragraph}">
                                <Setter Property="TextAlignment" Value="Right"/>
                                <Setter Property="Margin" Value="0" />
                                </Style>
                            </RichTextBox.Resources>

                        <FlowDocument>
                            <Paragraph>test <Bold>test111</Bold></Paragraph>
                        </FlowDocument>
                        </RichTextBox>
                        <local:CustomControl1 
                            MyProperty="{Binding MSG}"
                            />
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

also it is my viewmodel

    public class TestListViewModel : ViewModelBase
    {

        private ObservableCollection<TestMsgModel> _dataList = new ObservableCollection<TestMsgModel>();
        public ObservableCollection<TestMsgModel> DataList
        {
            get => _dataList;
            set
            {
                _dataList = value;
                OnPropertyChanged("DataList");
            }
        }
        public void OnPropertyChanged(string PropertyName)
        {
            if (PropertyChangedHandler != null)
            {
                PropertyChangedHandler(this, new PropertyChangedEventArgs(PropertyName));
            }
        }
        public TestListViewModel()
        {
            TestMsgModel testMsgModel = new TestMsgModel();
            testMsgModel.MSG = "TEST MSG 1";
            DataList.Add(testMsgModel);
        }

    }

When i remove the DependencyProperty in CustomControl, it works! enter image description here

But if not remove, there are some error.

How do i???

CodePudding user response:

Thank you, Clemens

I just changed "new PropertyMetadata(0));" to "new PropertyMetadata(null));"

        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MyPropertyProperty =
            DependencyProperty.Register("MyProperty", typeof(string), typeof(CustomControl1), new PropertyMetadata(0));

Change to

        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MyPropertyProperty =
            DependencyProperty.Register("MyProperty", typeof(string), typeof(CustomControl1), new PropertyMetadata(null));

It is solved.

  • Related