0
我正在關注MVVM,以便爲我的wpf應用程序項目創建不同的視圖/ usercontrols,以便爲不同的選項卡控件項目讀取來自Restful API的數據。爲什麼我的Mousedownclick事件畫布控制不適用於此視圖?任何幫助都非常感謝。當我輸入並單擊不同的畫布控件時,「文本框」中的文本應相應更改。 注意:即使Mouseenter也不起作用?我的wpf應用程序中的Canvas控件上的鼠標按下事件不起作用
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);
}
}*/
}
}
}
}
謝謝我會嘗試相同的 – Sikha
我不相信我沒有注意到,它現在工作正常謝謝:) – Sikha