【问题标题】:WPF Click on userControlWPF 点击用户控件
【发布时间】:2015-10-14 13:08:06
【问题描述】:

我想让容器窗口内的任何网格行都可以点击。 每行都是一个名为“MyBookControl”的用户控件。 单击 userControl 时,我想提出命令“DownloadCommand”。 我正在使用 mvvm 模式。

容器视图:https://onedrive.live.com/redir?resid=3A8F69A0FB413FA4!124&authkey=!ANdfYAk6f0Vf-8s&v=3&ithint=photo%2cpng

BookControl:

<UserControl  x:Name="MyBookControl" />
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>

        <Grid.Style>
            <Style TargetType="{x:Type Grid}">
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="False">
                        <Setter Property="Opacity" Value="0.6"></Setter>
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Opacity" Value="1"></Setter>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Grid.Style>

        <Label Grid.Row="0">Title</Label>
        <Label Grid.Row="1">Author</Label>
        <Label Grid.Row="2">Description</Label>

        <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Title}"/>
        <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Author}"/>
        <TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding Description}"/>
        <Button Grid.Column="2" Grid.RowSpan="3" Command="{Binding DownloadCommand}" Content="Download" />


        <Ellipse   Grid.Column="3" 
                    Height="20" Width="20"  
                    Stroke="Black" 
                    StrokeThickness="0.5" 
                    HorizontalAlignment="Center" 
                   Grid.Row="1"
                   />
        <Controls:PieSlice Grid.Column="3" Grid.Row="1" Stroke="Black" Fill="Black" 
                             Height="20" Width="20" 
                             StartAngle="0" EndAngle="{Binding Percent}"
                             HorizontalAlignment="Center" />
    </Grid>

</UserControl>

【问题讨论】:

  • 您可以尝试将MouseBinding 添加到控件的InputBindings 集合中:&lt;MouseBinding Gesture="LeftClick" Command="{Binding DownloadCommand}" /&gt;
  • 整个用户控件而不仅仅是下载按钮?

标签: wpf mvvm controltemplate


【解决方案1】:

据我了解,您希望构建一个响应式面板来容纳所有收藏成员。您可以尝试使用 listView 控件,并将您的用户控件作为 ListViewItem 的项目模板。那里的房子有选择功能(ListView)。这样,ListView 可以通过一堆对象绑定到您的主视图模型,并且每个用户控件都可以绑定到单个对象。

     <ListView ItemsSource="{Binding ToYourSourceCollection}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <YourUserControl/>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

然后,您可以构建一个处理 ListView 事件的行为,或者只监听 Selected 属性的变化。

【讨论】:

  • A ListBox 也适用于此。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多