我是 WPF MVVM 开发的初学者,只需要一些帮助。我只想知道如何在自定义控件或用户控件中的特定控件上添加依赖属性。
我有一个使用自定义自动完成控件 (UserAutoComplete) 的视图 (UserMaintenance)。
用户维护视图.xaml
<myProject:UserAutoComplete ViewModel="{Binding ViewModel}" SelectedItem="{max:ValidationBinding SelectedCandidate}" IsRequired="True"/>
用户自动完成.xaml
<Controls:AutoCompleteTextBox
FontSize="11"
x:Name="PART_AutoCompleteTextBox"
SelectedItem="{Binding ViewModel.SelectedItem, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Controls:UserAutoComplete}}, Converter={StaticResource UserSelectorConverter}, ConverterParameter={x:Type Entities:User}, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}"
Text="{Binding ViewModel.Text, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Controls:UserAutoComplete}}, UpdateSourceTrigger=PropertyChanged}"
ItemsSource="{Binding ViewModel.AutoCompleteItems, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Controls:UserAutoComplete}}}"
SearchCommand="{Binding ViewModel.AutoCompleteSearchCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Controls:UserAutoComplete}}}"
IsRequired="{TemplateBinding IsRequired}"
BorderBrush="{TemplateBinding BorderBrush}"
IsItemSelected="{Binding ViewModel.IsItemSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Controls:UserAutoComplete}}}"
IsPickerOpen="{Binding ViewModel.IsPickerOpen, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Controls:UserAutoComplete}}, Mode=TwoWay}"
IsListOpen="{Binding ViewModel.IsListOpen, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Controls:UserAutoComplete}}, Mode=TwoWay}"
Focusable="True"
Behaviours:TextCapitalizationBehaviorBinding.TextCapitalization="Title"
IsReadOnly="{TemplateBinding IsReadOnly}"
DisplayMember="DisplayName">
<Controls:AutoCompleteTextBox.AutoCompleteColumns>
<telerik:GridViewDataColumn UniqueName="DisplayName" Header="Name" DataMemberBinding="{Binding DisplayName}" DataType="{x:Type System:String}" Width="0.4*">
<telerik:GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding DisplayName, Mode=OneTime}" Behaviours:HighlightTextBehaviorBinding.Regex="{Binding
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Controls:UserAutoComplete}},
Converter={StaticResource RegexEscapeConverter},
Path=ViewModel.LastAutoCompleteSearchText}" />
</DataTemplate>
</telerik:GridViewColumn.CellTemplate>
</telerik:GridViewDataColumn>
<telerik:GridViewDataColumn Header="Date of birth" DataMemberBinding="{Binding DateOfBirth}" DataType="{x:Type System:String}" Width="Auto">
<telerik:GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock TextWrapping="Wrap" Text="{Binding DateOfBirth, StringFormat=' {0:d} ', Mode=OneWay}" >
</TextBlock>
</DataTemplate>
</telerik:GridViewColumn.CellTemplate>
</telerik:GridViewDataColumn>
<telerik:GridViewDataColumn Header="Suburb" DataMemberBinding="{Binding Suburb}" DataType="{x:Type System:String}" Width="Auto">
<telerik:GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock TextWrapping="Wrap" Text="{Binding Suburb, Mode=OneWay}" >
</TextBlock>
</DataTemplate>
</telerik:GridViewColumn.CellTemplate>
</telerik:GridViewDataColumn>
</Controls:AutoCompleteTextBox.AutoCompleteColumns>
现在,我想仅针对特定视图修改列的宽度和 Telerik GridViewDataColums 标题的背景颜色。
不用说,还有很多其他的视图也使用 UserAutoComplete,所以肯定触及 UserAutoComplete 代码并不是理想的解决方案。
所以这里出现了依赖属性。我的问题是我应该使用什么,正常的依赖属性还是附加的依赖?以及将依赖属性放在 UserControl 上的什么位置,或者我可以将它放在 View 中吗?
像这样的东西......
public class UserAutoComplete : AutoComplete
{
public int WidthOfGridDataColumn
{
get { return (int)GetValue(WidthOfGridDataColumnProperty); }
set { SetValue(WidthOfGridDataColumnProperty, value); }
}
// Using a DependencyProperty as the backing store for WidthOfGridDataColumn. This enables animation, styling, binding, etc...
public static readonly DependencyProperty WidthOfGridDataColumnProperty =
DependencyProperty.Register("WidthOfGridDataColumn", typeof(int), typeof(ownerclass), new UIPropertyMetadata(0));
public string BackgroundColorOfGridHeader
{
get { return (string)GetValue(BackgroundColorOfGridHeaderProperty); }
set { SetValue(BackgroundColorOfGridHeaderProperty, value); }
}
// Using a DependencyProperty as the backing store for BackgroundColorOfGridHeader. This enables animation, styling, binding, etc...
public static readonly DependencyProperty BackgroundColorOfGridHeaderProperty =
DependencyProperty.Register("BackgroundColorOfGridHeader", typeof(string), typeof(ownerclass), new UIPropertyMetadata(0));`
提前致谢。