我正在关注我的wpf应用程序项目的MVVM,并为不同的选项卡控件项创建不同的视图/用户控件,从Restful API读取数据。为什么画布控件上的Mousedownclick事件不适用于此视图?任何帮助都是当我进入并单击不同的画布控件时,“文本框”中的文本应该相应地改变。 注意:即使Mouseenter也无效?
XAML
<UserControl x:Class="ISTE.Views.Resources"
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:ISTE.Views"
mc:Ignorable="d"
d:DesignHeight="600" d:DesignWidth="1300">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="400"/>
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Name="go" Click="Button_Click" Margin="56,68,714,506" Grid.ColumnSpan="2" >RIT Website</Button>
<StackPanel Grid.Column="1">
<WebBrowser Height="600px" x:Name="ritwebsite" Margin="117,0,254,0"/>
</StackPanel>
<Canvas Grid.Column="2" Height="400" Width="400">
<Canvas Name="studyAbroad" MouseDown="studyAbroad_MouseDown" Height="132" Width="136" Canvas.Top="10" Canvas.Left="10" Background="Blue"/>
<Canvas Name="studentServices" MouseDown="studyAbroad_MouseDown" Height="150" Width="154" Canvas.Top="200" Canvas.Left="208" Background="SkyBlue"/>
<Canvas Name="studentAmbassadors" MouseDown="studyAbroad_MouseDown" Height="150" Width="158" Canvas.Top="100" Canvas.Left="100" Background="DarkBlue"/>
</Canvas>
<TextBlock Grid.Column="2" x:Name="res"><Run Text="ADJH"/><InlineUIContainer>
<TextBox x:Name="textBox" Height="51" TextWrapping="Wrap" Text="TextBox" Width="172" RenderTransformOrigin="0.924,8">
<TextBox.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX="-1"/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</TextBox.RenderTransform>
</TextBox>
</InlineUIContainer></TextBlock>
</Grid>
</UserControl>
----------------------------------------------------------------------------
CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ISTE.ViewModels;
using ISTE.Models;
namespace ISTE.Views
{
/// <summary>
/// Interaction logic for Resources.xaml
/// </summary>
public partial class Resources : UserControl
{
ResourcesVM vm;
public Resources()
{
InitializeComponent();
vm = new ResourcesVM();
this.DataContext = vm;
vm.GetData();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.ritwebsite.Navigate(new Uri("https://www.rit.edu"));
}
private void studyAbroad_MouseDown(object sender, MouseButtonEventArgs e)
{
{
var b = sender as Canvas;
var selected = b.Name;
var resselected = b.Name.ToString();
if (resselected == "studyAbroad")
{
res.Text = "";
res.Text = vm.ItemData.studyAbroad.description;
}
else if (resselected == "studentServices")
{
res.Text = "";
res.Text = vm.ItemData.studentServices.title;
}
else
{
res.Text = "";
res.Text = vm.ItemData.studentAmbassadors.title;
}
/* foreach (Place p in vm.ItemData.studyAbroad.places)
{
if (p.nameOfPlace == selected)
{
textBox.Text = "";
textBox.Text = p.description + "\n\n";
Console.WriteLine("places \t" + p.nameOfPlace);
}
}*/
}
}
}
}
答案 0 :(得分:0)
这很简单,实际上与MVVM无关。
查看XAML定义,您的TextBlock
占据了第二列的所有可用空间,画布位于其下方,因此无法响应鼠标点击。
将合适的尺寸设置为TextBlock
并将其移动到其中一个角可以解决您的问题。
或者您可以记录控件的Z顺序,首先是TextBlock,然后是画布。