在使用代码时替换自定义控件的子元素



我有一个可重用的搜索结果自定义控件。它使用 ListView 网格视图来显示搜索结果,并且已在我的应用中的多个位置使用。

<views:AbstractDictionaryPickerView x:Class="MyApp.Common.Controls.Dictionaries.Views.AbstractDictionaryPickerView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
             xmlns:watermark="clr-namespace:MyApp.Common.Controls.Watermark"
             xmlns:behaviors="clr-namespace:MyApp.Common.Behaviors"
             xmlns:listViewLayout="clr-namespace:Itenso.Windows.Controls.ListViewLayout;assembly=Itenso.Windows.Controls.ListViewLayout"
             xmlns:views="clr-namespace:MyApp.Common.Controls.Dictionaries.Views"
             xmlns:viewModels="clr-namespace:MyApp.Common.Controls.Dictionaries.ViewModels"
             xmlns:design="clr-namespace:MyApp.Common.Controls.Dictionaries.ViewModels.Design"
             d:DataContext="{design:DesignMultiDictionaryPickerViewModel}"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Viewbox Stretch="Fill">
            <Canvas Width="800" Height="800">
                <Rectangle Fill="#ffffffff" Width="800" Height="800" />
                <Rectangle Width="5" Height="800" >
                    <Rectangle.Fill>
                        <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
                            <GradientStop Color="#FFCAEBF4"/>
                            <GradientStop Color="#FFCEF5FF" Offset="1"/>
                        </LinearGradientBrush>
                    </Rectangle.Fill>
                </Rectangle>
            </Canvas>
        </Viewbox>
        <Grid Background="White">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="1*" />
                </Grid.RowDefinitions>
                <TextBox
                    Grid.Row="0" Height="Auto"
                            Margin="5,0,5,0"
                            Style="{StaticResource TextBoxStyle}"
                            Text="{Binding SearchQuery, UpdateSourceTrigger=PropertyChanged}">
                    <i:Interaction.Behaviors>
                        <watermark:TextBoxWatermarkBehavior Label="{Binding WatermarkText, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}" 
                                                            LabelStyle="{StaticResource WatermarkStyle}"/>
                        <behaviors:SelectAllWhenTextBoxFocusedBehavior/>
                        <behaviors:TextBoxArrowUpDownNavigationBehavior/>
                        <behaviors:SetLogicalFocusBehavior/>
                    </i:Interaction.Behaviors>
                </TextBox>
                <Grid Grid.Row="1">
                    <ListView listViewLayout:ListViewLayoutManager.Enabled="True" x:Name="SearchResultsList"
                                      ItemsSource="{Binding FilteredElements}"
                                      ScrollViewer.VerticalScrollBarVisibility="Auto" Margin="5,2,5,3"                      
                                      SelectionMode="Single"
                                      >
                        <ListView.Resources>
                            <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource TextBlockStyle}"/>
                        </ListView.Resources>
                        <i:Interaction.Behaviors>
                            <behaviors:ArrowNavigationBehavior/>
                            <behaviors:AutoSizeListViewColumns/>
                        </i:Interaction.Behaviors>
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="PreviewMouseDoubleClick">
                                <i:InvokeCommandAction Command="{Binding ChooseItemCommand}" 
                                        CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}, Path=SelectedItem}"/>
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                        <ListView.InputBindings>
                            <KeyBinding Key="Enter"                                        
                                                Command="{Binding ChooseItemCommand}" 
                                                CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}, Path=SelectedItem}" />
                            <MouseBinding MouseAction="LeftDoubleClick"
                                            Command="{Binding ChooseItemCommand}" 
                                            CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}, Path=SelectedItem}" />
                        </ListView.InputBindings>
                        <ListView.View>
                            <!-- GRIDVIEW TO REPLACE -->
                            <GridView ColumnHeaderContainerStyle="{StaticResource {x:Type GridViewColumnHeader}}" x:Name="ElementsGridView">
                                <GridViewColumn Header="">
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate DataType="viewModels:ChoosableViewModel">
                                            <CheckBox IsChecked="{Binding IsChosen}"/>
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>
                            </GridView>
                        </ListView.View>
                    </ListView>
                </Grid>
            </Grid>
        </Grid>
</views:AbstractDictionaryPickerView>

使用代码如下所示:

<UserControl x:Class="MyApp.Modules.Management.OnlineRegistrationSettings.Tabs.AvailableDoctors.OnlineRegistrationDoctorsSettingsView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:views="clr-namespace:MyApp.Common.Controls.Dictionaries.Views;assembly=MyApp.Common"
             xmlns:onlineRegistrationSettings="clr-namespace:MyApp.Modules.Management.OnlineRegistrationSettings"
             d:DataContext="{d:DesignInstance onlineRegistrationSettings:OnlineRegistrationSettingsViewModel}"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Label Style="{StaticResource LabelStyle}" Content="Wybór lekarzy, którzy mają być dostępni w rejestracji online."/>

        <!-- HERE -->
        <views:AbstractDictionaryPickerView DataContext="{Binding MultiDictionaryPickerViewModel}" Grid.Row="1"
                                         Configuration="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}, Path=DataContext.Configuration}"/>
    </Grid>
</UserControl>

现在我想修改GridView列(使用 ComboBox 添加一个新列)。

是否可以在使用代码的 XAML 中重写views:AbstractDictionaryPickerViewGridView 元素?我的意思是第一个片段中<!-- GRIDVIEW TO REPLACE -->标签下方的内容。

是否可以在使用代码的 XAML 中重写视图的 GridView 元素:抽象字典PickerView?

不。至少不要只使用 XAML。

但是,如果将一个属性添加到 AbstractDictionaryPickerView 控件中,该控件公开了 ListView 的视图:

public partial class AbstractDictionaryPickerView : UserControl
{
    public AbstractDictionaryPickerView()
    {
        InitializeComponent();
    }
    public ViewBase GridView
    {
        get { return SearchResultsList.View; }
        set { SearchResultsList.View = value; }
    }
}

。可以在使用 XAML 标记中将其设置为新的 GridView:

<views:AbstractDictionaryPickerView>
    <views:AbstractDictionaryPickerView.GridView>
        <GridView>
            <GridView.Columns>
                <GridViewColumn Header="..." DisplayMemberBinding="{Binding}" />
            </GridView.Columns>
        </GridView>
    </views:AbstractDictionaryPickerView.GridView>
</views:AbstractDictionaryPickerView>

最新更新