WPF将鼠标悬停在唯一的图片上

时间:2017-05-18 23:32:13

标签: wpf image mvvm data-binding

我想创建一个Magic the Gathering交易用户界面,如果用户将鼠标悬停在特定的卡片名称上,它将显示该卡片的图像(见图像)。目前,在MultiMasterCardListView中,唯一显示的图像是黑色莲花卡图像,我对该图像路径进行了硬编码。因此,我悬停在哪个卡名称无关紧要,它只显示黑莲花图像。

所以我在MultiMasterCardListViewModel中创建了一个属性(PlayerCardPath),它将遍历数据库中的所有卡片 并测试悬停超过的内容/值是否等于该卡名称。如果是,则返回具有卡片名称和图像格式的路径。问题是mc.Name(来自MultiMasterCardViewModel)没有返回任何内容,因此" if" foreach内部的声明根本没有被击中。如何从MultiMasterCardListView获取悬停的值?如果您需要澄清,请告诉我。感谢enter image description here

MultiMasterCardListView:

<UserControl x:Class="OrderEntrySystem.MultiMasterCardListView"
         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:local="clr-namespace:OrderEntrySystem"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="600">
<UserControl.Resources>
    <ResourceDictionary Source="SharedResources.xaml" />
</UserControl.Resources>

<Grid>
    <DockPanel>
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" DockPanel.Dock="Top" Margin="0,0,0,5" Height="30">
            <TextBox x:Name="searchTextBox" Text="{Binding Path=SearchText}" Width="100" Height="23" Margin="0,4,5,0" />
            <ContentControl Content="{Binding Path=FilterCommands}" ContentTemplate="{StaticResource ResourceKey=NestedCommandTemplate}" />
        </StackPanel>
        <Grid DockPanel.Dock="Bottom">
                <local:SelectedItemsView />
            </Grid>
        <Grid DockPanel.Dock="Bottom">
            <local:PagingView></local:PagingView>
        </Grid>
        <ListView ItemsSource="{Binding Path=SortedCards}" ItemContainerStyle="{StaticResource ResourceKey=ListViewStyle}">
            <ToolTipService.ToolTip>
                <Border>
                    <StackPanel>
                        <Image Source="C:\Users\Teng\Documents\0515-392-team-orange\Team Magic Project\OrderEntrySystem\Images\Black Lotus.JPG"/>
                    </StackPanel>
                </Border>
            </ToolTipService.ToolTip>
            <ListView.View>
                <GridView>
                    <GridViewColumn Width="175" DisplayMemberBinding="{Binding Path=Name}">
                        <GridViewColumnHeader Content="Name" Command="{Binding Path=SortCommand}" CommandParameter="Name" />
                    </GridViewColumn>
                    <GridViewColumn Header="Point Value" Width="70" DisplayMemberBinding="{Binding Path=PointValue}" />
                </GridView>
            </ListView.View>
        </ListView>
    </DockPanel>
</Grid>

MultiMasterCardListViewModel:

        public string PlayerCardPath
    {
        get
        {
            string result = null;
            MultiMasterCardListView mC = new MultiMasterCardListView();

            foreach (var card in this.repository.GetMasterCardLists())
            {
                if (card.Name == mC.Name)
                {
                    result = (@"..\..\..\Images\" + mC.Name + ".JPG");
                    break;
                }
            }
            return result;
        }
    }

SharedResources:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:OrderEntrySystem">

<DataTemplate DataType="{x:Type local:MasterCardListViewModel}">
    <local:MasterCardListView></local:MasterCardListView>
</DataTemplate>

<DataTemplate DataType="{x:Type local:MemberViewModel}">
    <local:MemberView></local:MemberView>
</DataTemplate>

<DataTemplate DataType="{x:Type local:MultiMasterCardListViewModel}">
    <local:MultiMasterCardListView></local:MultiMasterCardListView>
</DataTemplate>

<DataTemplate DataType="{x:Type local:MultiMemberViewModel}">
    <local:MultiMemberView></local:MultiMemberView>
</DataTemplate>

<DataTemplate DataType="{x:Type local:MultiMemberCardListViewModel}">
    <local:MultiMemberCardListView></local:MultiMemberCardListView>
</DataTemplate>

<DataTemplate DataType="{x:Type local:TransactionViewModel}">
    <local:TransactionView></local:TransactionView>
</DataTemplate>

<DataTemplate DataType="{x:Type local:MultiTransactionViewModel}">
    <local:MultiTransactionView></local:MultiTransactionView>
</DataTemplate>

<DataTemplate DataType="{x:Type local:MultiCardTransactionViewModel}">
    <local:MultiCardTransactionView></local:MultiCardTransactionView>
</DataTemplate>

<DataTemplate DataType="{x:Type local:CardConditionViewModel}">
    <local:CardConditionView></local:CardConditionView>
</DataTemplate>

<DataTemplate DataType="{x:Type local:QuantityViewModel}">
    <local:QuantityView></local:QuantityView>
</DataTemplate>

<DataTemplate DataType="{x:Type local:PurchasePointsViewModel}">
    <local:PurchasePointsView></local:PurchasePointsView>
</DataTemplate>

<DataTemplate DataType="{x:Type local:MultiMyCardListViewModel}">
        <local:MultiMyCardListView></local:MultiMyCardListView>
</DataTemplate>

<DataTemplate DataType="{x:Type local:MultiShoppingCartViewModel}">
    <local:MultiShoppingCartView></local:MultiShoppingCartView>
</DataTemplate>

<DataTemplate DataType="{x:Type local:ShoppingCartViewModel}">
    <local:ShoppingCartView></local:ShoppingCartView>
</DataTemplate>

<DataTemplate DataType="{x:Type local:ReportViewModel}">
    <local:ReportView></local:ReportView>
</DataTemplate>

<DataTemplate DataType="{x:Type local:LoginViewModel}">
    <local:LoginView></local:LoginView>
</DataTemplate>

<DataTemplate x:Key="WorkspacesTemplate">
    <TabControl IsSynchronizedWithCurrentItem="True" Margin="4" ItemsSource="{Binding}">
        <TabControl.Background>
            <ImageBrush ImageSource="..\..\Images\Magicthegathering-logo.svg.png" Stretch="Uniform" AlignmentY="Bottom" AlignmentX="Center" Opacity="0.5"/>
        </TabControl.Background>
        <TabControl.ItemTemplate>
            <DataTemplate>
                <DockPanel>
                    <Button Command="{Binding Path=CloseCommand}" Content="X" Cursor="Hand" Margin="4,0,0,0" FontWeight="Bold" Height="16" Width="16" FontFamily="Courier" FontSize="9" DockPanel.Dock="Right" />
                    <ContentPresenter Content="{Binding Path=DisplayName}" VerticalAlignment="Center" />
                </DockPanel>
            </DataTemplate>
        </TabControl.ItemTemplate>
    </TabControl>
</DataTemplate>

<DataTemplate x:Key="NestedCommandTemplate">
    <ItemsControl ItemsSource="{Binding}" HorizontalAlignment="Right">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Button Command="{Binding Path=Command}" Content="{Binding Path=DisplayName}" IsDefault="{Binding Path=IsDefault}" IsCancel="{Binding Path=IsCancel}" Height="23" Width="75" Margin="4,4,0,0" />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</DataTemplate>

<DataTemplate x:Key="CommandsTemplate">
    <ItemsControl ItemsSource="{Binding}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Button Command="{Binding Path=Command}" Content="{Binding Path=DisplayName}" />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</DataTemplate>

<ControlTemplate x:Key="HoverOverImage">
    <Border Name="border" BorderThickness="0" 
            Background="Transparent">
        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
    </Border>
    <ControlTemplate.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background">
                <Setter.Value>
                    <ImageBrush ImageSource="C:\Users\Teng\Documents\0515-392-team-orange\Team Magic Project\Images\Coalition Honor Guard.JPG" />
                </Setter.Value>
            </Setter>
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

<DataTemplate x:Key="HorizontalCommandsTemplate">
    <ItemsControl ItemsSource="{Binding}" HorizontalAlignment="Right">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Button Command="{Binding Path=Command}" Content="{Binding Path=DisplayName}" Height="23" Width="75" Margin="4,4,0,0" />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</DataTemplate>

<Style x:Key="MainHeaderStyle" TargetType="{x:Type HeaderedContentControl}">
    <Setter Property="HeaderTemplate">
        <Setter.Value>
            <DataTemplate>
                <Border Background="{StaticResource Brush_HeaderBackground}" BorderBrush="LightGray" BorderThickness="1" CornerRadius="5" Margin="4" Padding="4" SnapsToDevicePixels="True">
                    <TextBlock FontSize="14" FontWeight="Bold" Foreground="White" HorizontalAlignment="Center" Text="{TemplateBinding Content}" />
                </Border>
            </DataTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="IsTabStop" Value="False" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type HeaderedContentControl}">
                <DockPanel>
                    <ContentPresenter DockPanel.Dock="Top" ContentSource="Header" ContentTemplate="{TemplateBinding HeaderTemplate}" />
                    <ContentPresenter ContentSource="Content" ContentTemplate="{TemplateBinding ContentTemplate}" />
                </DockPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style x:Key="ListViewStyle" TargetType="{x:Type ListViewItem}">
    <Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}" />
</Style>

<Style x:Key="ValidationStyleTextBox" TargetType="{x:Type TextBox}">
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="True">
            <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors).CurrentItem.ErrorContent}" />
            <Setter Property="Validation.ErrorTemplate">
                <Setter.Value>
                    <ControlTemplate>
                        <DockPanel LastChildFill="true">
                            <TextBlock DockPanel.Dock="Right" VerticalAlignment="Center" Margin="5,0,0,0" Text="{Binding ElementName=customAdorner, Path=AdornedElement.(Validation.Errors).CurrentItem.ErrorContent}" />
                            <Border Background="Red" DockPanel.Dock="Right" Margin="5,0,0,0" Width="20" Height="20" CornerRadius="10">
                                <TextBlock Text="!" VerticalAlignment="Center" HorizontalAlignment="Center" FontWeight="Bold" Foreground="White" />
                            </Border>
                            <AdornedElementPlaceholder Name="customAdorner" VerticalAlignment="Center">
                                <Border BorderBrush="Red" BorderThickness="1" />
                            </AdornedElementPlaceholder>
                        </DockPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Trigger>
    </Style.Triggers>
</Style>

<Style x:Key="PagingButton" TargetType="{x:Type Button}">
    <Setter Property="FontFamily" Value="Webdings" />
    <Setter Property="Height" Value="23" />
    <Setter Property="Width" Value="45" />
    <Setter Property="Margin" Value="4,0,4,0" />
</Style>

<Style x:Key="PagingTextBlock" TargetType="{x:Type TextBlock}">
    <Setter Property="Margin" Value="4,0,4,0" />
    <Setter Property="VerticalAlignment" Value="Center" />
</Style>

0 个答案:

没有答案