繁体   English   中英

在 DataTemplate 中使用自定义控件绑定

[英]Use Custom Control binding in DataTemplate

我只想对渲染性能进行一些自定义控制。 并绑定我的数据

制作自定义控件是可以的,但我无法为数据绑定设置 dependancyProperty。

我做了一个自定义控制文件

自定义控件1.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));


    }

并设置我的 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>

并在我看来使用它

        <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>

这也是我的视图模型

    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);
        }

    }

当我删除 CustomControl 中的 DependencyProperty 时,它起作用了! 在此处输入图像描述

但如果不删除,就会出现一些错误。

我如何能???

谢谢你,克莱门斯

我刚刚更改了“new PropertyMetadata(0))”; 到“新的 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));

改成

        // 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));

它被解决了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM