我是WPF MVVM开发的初学者,只需要一些关于这个的帮助。我只想知道如何在自定义控件或用户控件内的特定控件上添加依赖项属性。
我有一个使用自定义自动完成控件(UserAutoComplete)的视图(UserMaintenance)。
UserMaintenanceView.xaml
<myProject:UserAutoComplete ViewModel="{Binding ViewModel}" SelectedItem="{max:ValidationBinding SelectedCandidate}" IsRequired="True"/>
UserAutoComplete.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标题的列WIDTH和BACKGROUNDCOLOR。
毋庸置疑,还有许多其他视图也使用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));`
提前致谢。