0

我有一个具有列表框的用户控件。我将此 UserControl 放在 Mainpage.xaml 上。我想处理 Mainpage.xaml.cs 而不是 usercontrol.xaml.cs 上的选定和按钮单击事件,因为我将使用此 UserControl 到另一个页面并且响应会有所不同。我该怎么做?你能提供给我的例子或链接吗?提前致谢。

有我的用户控件:

<UserControl x:Class="CMSPhoneApp.QueueListControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="480" d:DesignWidth="480"
xmlns:local="clr-namespace:CMSPhoneApp"  >

<UserControl.Resources>
    <Style x:Key="ScrollViewerStyle1" TargetType="ScrollViewer">
        <Setter Property="VerticalScrollBarVisibility" Value="Auto"/>
        <Setter Property="HorizontalScrollBarVisibility" Value="Disabled"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Padding" Value="0"/>
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ScrollViewer">
                    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="ScrollStates">
                                <VisualStateGroup.Transitions>
                                    <VisualTransition GeneratedDuration="00:00:00.5"/>
                                </VisualStateGroup.Transitions>
                                <VisualState x:Name="Scrolling">
                                    <Storyboard>
                                        <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="VerticalScrollBar"/>
                                        <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="HorizontalScrollBar"/>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="NotScrolling"/>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Grid Margin="{TemplateBinding Padding}">
                            <ScrollContentPresenter x:Name="ScrollContentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}"/>
                            <ScrollBar x:Name="VerticalScrollBar" HorizontalAlignment="Right" Height="Auto" IsHitTestVisible="False" IsTabStop="False" Maximum="{TemplateBinding ScrollableHeight}" Minimum="0" Opacity="0" Orientation="Vertical" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Value="{TemplateBinding VerticalOffset}" ViewportSize="{TemplateBinding ViewportHeight}" VerticalAlignment="Stretch" Width="5" BorderBrush="#FF2022BC"/>
                            <ScrollBar x:Name="HorizontalScrollBar" HorizontalAlignment="Stretch" Height="5" IsHitTestVisible="False" IsTabStop="False" Maximum="{TemplateBinding ScrollableWidth}" Minimum="0" Opacity="0" Orientation="Horizontal" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" Value="{TemplateBinding HorizontalOffset}" ViewportSize="{TemplateBinding ViewportWidth}" VerticalAlignment="Bottom" Width="Auto"/>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>


    <local:VisibilityConverter x:Key="VisibilityConverter"/>
    <local:ColumSpanConverter x:Key="ColumSpanConverter"/>
</UserControl.Resources>


<Grid x:Name="LayoutRoot"  Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>

    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

        <ScrollViewer Style="{StaticResource ScrollViewerStyle1}" Background="#00E23162">
            <ListBox x:Name="lstCall" HorizontalAlignment="Left" Margin="6,6,0,0"   VerticalAlignment="Top" Width="400" SelectionChanged="lstCall_SelectionChanged"    >
                <ListBox.ItemTemplate>
                    <DataTemplate>

                        <Grid  x:Name="grdQueue" Background="Transparent">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="70"/>                                   
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="35" />                                        
                                <ColumnDefinition Width="100*"/>
                                <ColumnDefinition Width="125" />
                            </Grid.ColumnDefinitions>
                            <Image Source="{Binding Type}" Grid.Row="0" Grid.Column="0"/>
                            <TextBlock  Grid.Row="0" Grid.Column="1"  Grid.ColumnSpan= "{Binding isSpan, Converter={StaticResource ColumSpanConverter}}" Text="{Binding summary}" TextWrapping="Wrap"
                                             Style="{StaticResource PhoneTextAccentStyle}"  />
                            <Button Grid.Row="0" Grid.Column="3" ClickMode="Press" Click="Action_Click" Style="{StaticResource ButtonStyle1}"
                                            Visibility="{Binding isVisibility, Converter={StaticResource VisibilityConverter}}">
                                <Button.Content>
                                    <TextBlock Width="85" Height="70" Text="{Binding ActionCaption}" 
                                                       Style="{StaticResource LabelStyle_20}"     />
                                </Button.Content>
                            </Button>

                        </Grid>

                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </ScrollViewer>
    </Grid>
</Grid>
</UserControl>

有在 Mainpage.xaml 上添加 UserControl 的代码:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <StackPanel>
            <my:QueueListControl />
            </StackPanel>
    </Grid>
</Grid>

我可以通过以下代码在 MainPage.xaml.cs 上的 lst 上绑定数据:

MyQueue = new List<QueueItem>();
        MyQueue.Add(new QueueItem { summary = "Test1tqeewwewew332233 3233322323 wdqwqwqwqwq", status = "Open", callNumber = "1" });
        MyQueue.Add(new QueueItem { summary = "Test1tqeewwewew332233 3233322323 wdqwqwqwqwq", status = "Responded", callNumber = "2" });
        MyQueue.Add(new QueueItem { summary = "Test1tqeewwewew332233 3233322323 wdqwqwqwqwq", status = "Resolved", callNumber = "3" });
        MyQueue.Add(new QueueItem { summary = "Test1tqeewwewew332233 3233322323 wdqwqwqwqwq", status = "transfer", callNumber = "4" });
        MyQueue.Add(new QueueItem { summary = "Test1tqeewwewew332233 3233322323 wdqwqwqwqwq", status = "Complete", callNumber = "5" });
        test.lst.ItemsSource = MyQueue;
4

2 回答 2

0

一种解决方案是使用诸如MVVM Light 之类的库;它有一个 Messenger 类(教程),您可以使用它从后面的 UserControl 代码发送消息。

在 MainPage 的OnNavigatedTo方法中注册 UserControl 的消息,并在“OnNavigatedFrom”方法中取消注册以收听消息。您可以在要使用此 UserControl 的其他页面中执行相同操作。

UserControl 触发的消息必须包含任一类对其做出适当反应所需的所有相关信息。因此,您可以将控件的代码隐藏与用户交互逻辑分离。

于 2012-08-01T23:31:03.233 回答
0

您需要UserControl通过公开其状态和事件来提高您的可重用性,以便这些可以由托管您的控件实例的控件更新/处理。有关详细信息,请参阅本教程:

在 WPF / Silverlight 中创建可重用用户控件的简单模式

于 2012-08-02T05:53:43.710 回答