WPF 点击用户控件

标签 wpf mvvm controltemplate

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

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

图书控制:

<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>

最佳答案

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

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

然后,您可以构建一个解决 ListView 事件的行为,或者只监听 Selected 属性更改。

关于WPF 点击用户控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33126206/

相关文章:

C# WPF Datagrid 如何禁用特定列上的选择单元格

c# - 带有Show.Busy()和Show.NotBusy()的Caliburn错误

c# - 如何将用户控件复选框绑定(bind)到列表框中

Xamarin.Forms 默认控件模板

c# - 为什么 DataTemplate.LoadContent() 不遵守模板定义的触发器?

c# - 虚拟化 TreeView 中的滚动问题

WPF ShowDialog 和 ElementHost

c# - 每次刷新都会增加更新私有(private) Observable Collection 的时间

WPF - 选定的 ListView 项目更改字体大小

c# - 如何使用 INamingContainer 创建自定义控件?