在 DataTemplate 中使用自定义控件绑定
Use Custom Control binding in DataTemplate
我只想对渲染性能进行一些自定义控制。
并绑定我的数据
制作自定义控件可以,但我无法为数据绑定设置 dependancyProperty。
我制作了自定义控制文件
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));
}
并设置我的 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>
也是我的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);
}
}
当我删除 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));
解决了
我只想对渲染性能进行一些自定义控制。 并绑定我的数据
制作自定义控件可以,但我无法为数据绑定设置 dependancyProperty。
我制作了自定义控制文件
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));
}
并设置我的 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>
也是我的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);
}
}
当我删除 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));
解决了