Xaml 具有可绑定集合的 WinRT 自定义用户控件

Xaml WinRT Custom User Control with Bindable Collection

我正在尝试创建一个自定义用户控件,我可以在我的所有视图中重复使用它。我的 BaseViewModel 有一个名为 ViewAlerts 的 属性,旨在用于在整个应用程序中一致地显示警报(例如成功的更新、失败的请求等)。我能够达到我的自定义控件是可构建的并且具有用于绑定的 属性 的地步,但是永远不会显示警报集合。为了测试目的,我在我的基本视图模型中静态定义了一些警报,并且无法让警报显示(似乎是 INotifyPropertyChanged 的​​问题,但我的可绑定 属性 继承自 ObservableCollection<> 所以它应该是自动处理我认为的)。

到目前为止,这是我的自定义控件:

    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="../../Resources/MMJCeoResources.xaml" />
                <ResourceDictionary>
                    <Style TargetType="TextBlock" x:Key="AlertTextStyle">
                        <Setter Property="FontSize" Value="15"></Setter>
                        <Setter Property="Margin" Value="10" />
                        <Setter Property="Padding" Value="10" />
                    </Style>
                    <DataTemplate x:Key="DangerAlert">
                        <Grid Background="LightPink">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="*" />
                                <RowDefinition Height="10*"/>
                            </Grid.RowDefinitions>
                            <Border Width="10"
                                    Height="10"
                                    BorderBrush="DarkRed"
                                    HorizontalAlignment="Right"
                                    Margin="5"
                                    Grid.Row="0">
                                    <TextBlock Text="X" />
                            </Border>
                            <TextBlock Grid.Row="1" Text="{Binding Message}" Style="{StaticResource AlertTextStyle}" Foreground="DarkRed"/>
                        </Grid>
                    </DataTemplate>
                    <DataTemplate x:Key="SuccessAlert">
                        <Grid Background="LightGreen">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="*" />
                                <RowDefinition Height="10*"/>
                            </Grid.RowDefinitions>
                            <Border Width="10"
                                    Height="10"
                                    BorderBrush="DarkGreen"
                                    HorizontalAlignment="Right"
                                    Margin="5"
                                    Grid.Row="0">
                                <TextBlock Text="X" />
                            </Border>
                            <TextBlock Grid.Row="1" Text="{Binding Message}" Style="{StaticResource AlertTextStyle}" Foreground="DarkGreen"/>
                        </Grid>
                    </DataTemplate>
                    <DataTemplate x:Key="InfoAlert">
                        <Grid Background="LightGreen">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="*" />
                                <RowDefinition Height="10*"/>
                            </Grid.RowDefinitions>
                            <Border Width="10"
                                    Height="10"
                                    BorderBrush="DarkGreen"
                                    HorizontalAlignment="Right"
                                    Margin="5"
                                    Grid.Row="0">
                                <TextBlock Text="X" />
                            </Border>
                            <TextBlock Grid.Row="1" Text="{Binding Message}" Style="{StaticResource AlertTextStyle}" Foreground="DarkGreen"/>
                        </Grid>
                    </DataTemplate>
                    <DataTemplate x:Key="WarningAlert">
                        <Grid Background="LightSalmon">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="*" />
                                <RowDefinition Height="10*"/>
                            </Grid.RowDefinitions>
                            <Border Width="10"
                                    Height="10"
                                    BorderBrush="DarkOrange"
                                    HorizontalAlignment="Right"
                                    Margin="5"
                                    Grid.Row="0">
                                <TextBlock Text="X" />
                            </Border>
                            <TextBlock Grid.Row="1" Text="{Binding Message}" Style="{StaticResource AlertTextStyle}" Foreground="DarkOrange"/>
                        </Grid>
                    </DataTemplate>
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>

    <Grid>
        <ItemsControl ItemsSource="{Binding Path=Alerts}"
                      ItemTemplateSelector="{StaticResource AlertDataTemplateSelector}">

        </ItemsControl>
    </Grid>
</UserControl>

控件的隐藏代码:

public sealed partial class AlertControl : UserControl
{
    public static readonly DependencyProperty AlertsProperty = DependencyProperty.Register(
        "Alerts", typeof (ObservableList<Alert>), typeof (AlertControl), new PropertyMetadata(default(ObservableList<Alert>), OnAlertsChanged));

    public ObservableList<Alert> Alerts
    {
        get { return (ObservableList<Alert>) GetValue(AlertsProperty); }
        set { SetValue(AlertsProperty, value); }
    }
    //I was trying to adapt another tutorial to what I was trying to accomplish but only got this far
    public static void OnAlertsChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        var old = e.OldValue as ObservableList<Alert>;
        var me = sender as AlertControl;

        if (old != null)
        {
            old.CollectionChanged -= me.OnAlertCollectionChanged;
        }

        var n = e.NewValue as ObservableList<Alert>;
        if (n != null)
            n.CollectionChanged += me.OnAlertCollectionChanged;
    }

    private void OnAlertCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        if (e.Action == NotifyCollectionChangedAction.Reset)
        {
            Alerts.Clear();
            var n = e.NewItems as ObservableList<Alert>;
            Alerts.AddRange(n);
        }


    }
    public AlertControl()
    {
        this.InitializeComponent();
        DataContext = this;

    }
}

示例实现:

 <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <StackPanel Orientation="Vertical" HorizontalAlignment="Stretch">
        <TextBlock Style="{StaticResource PageTitle}" Text="User Information" />
        <controls:AlertControl Alerts="{Binding ViewAlerts}" />

在此实现中,ViewAlerts 属性 中有 4 个静态定义的警报,因此我知道应该显示一些值。

您应该将 DataContext 提供给内部网格而不是控件本身,因为这样外部绑定将在控件内搜索您的 ViewAlerts

<Grid x:Name="InnerGrid">
    <ItemsControl ItemsSource="{Binding Path=Alerts}"
                  ItemTemplateSelector="{StaticResource AlertDataTemplateSelector}">

    </ItemsControl>
</Grid>


public AlertControl()
{
    this.InitializeComponent();
    InnerGrid.DataContext = this;
}

之后就可以绑定到Alerts了,Alerts会绑定到InnerGrid里面的ItemsControl