我在MainWindow.xaml中有一个WrapPanel,其中填充了(PlayerCard)用户控制子项。这些用户控件分别显示' Player.cs'属性。每个用户控件都有一个删除按钮,允许我删除WrapPanel中的一个PlayerCards。
我还有一个名为' playerList'的玩家对象列表。位于我的MainWindow.xaml.cs中,每次创建用户控件时我都会用它来写入/读取xml文件。
我所坚持的是如何在我的PlayerCard用户控件中单击我的删除按钮时修改MainWindow中的playerList。如何提醒MainWindow正在删除此用户控件?似乎解雇一个事件是合适的,但我不确定如何。
Player.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace BaseballLinker.Objects
{
public class Player
{
[XmlElement("Player Name")]
public string name { get; set; }
[XmlElement("MLB Link")]
public string mlb { get; set; }
[XmlElement("MiLB Link")]
public string milb { get; set; }
[XmlElement("ESPN Link")]
public string espn { get; set; }
[XmlElement("Baseball Referance Link")]
public string baseballref { get; set; }
[XmlElement("Fangraphs Link")]
public string fangraphs { get; set; }
}
}
MainWindow.xaml:
<Window x:Class="BaseballLinker.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="900" Width="1600" ResizeMode="CanMinimize">
<Grid>
<Grid.Background>
<ImageBrush ImageSource="Assets/Images/Background.jpg"/>
</Grid.Background>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="15*" />
<ColumnDefinition Width="3*" />
<ColumnDefinition Width="77*" />
<ColumnDefinition Width="4*" />
</Grid.ColumnDefinitions>
<Grid Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="14*" />
<RowDefinition Height="71*" />
<RowDefinition Height="15*" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="1">
<TextBlock Text="Add Player" FontSize="35" Foreground="White"/>
<TextBox x:Name="Name" Margin="0,10" Style="{StaticResource TextBoxStyle}" GotKeyboardFocus="Name_GotKeyboardFocus"/>
<TextBlock Text="Name" Margin="0,-15" FontSize="20" Foreground="White"/>
<TextBox x:Name="MLB" Margin="0,35" Style="{StaticResource TextBoxStyle}" GotKeyboardFocus="MLB_GotKeyboardFocus"/>
<TextBlock Text="MLB" Margin="0,-35" FontSize="20" Foreground="White"/>
<TextBox x:Name="MiLB" Margin="0,25" Style="{StaticResource TextBoxStyle}" GotKeyboardFocus="MiLB_GotKeyboardFocus"/>
<TextBlock Text="MiLB" Margin="0,-30" FontSize="20" Foreground="White"/>
<TextBox x:Name="ESPN" Margin="0,25" Style="{StaticResource TextBoxStyle}" GotKeyboardFocus="ESPN_GotKeyboardFocus"/>
<TextBlock Text="ESPN" Margin="0,-30" FontSize="20" Foreground="White"/>
<TextBox x:Name="BRef" Margin="0,25" Style="{StaticResource TextBoxStyle}" GotKeyboardFocus="BRef_GotKeyboardFocus"/>
<TextBlock Text="Baseball Referance" Margin="0,-30" FontSize="20" Foreground="White"/>
<TextBox x:Name="FG" Margin="0,25" Style="{StaticResource TextBoxStyle}" GotKeyboardFocus="FG_GotKeyboardFocus"/>
<TextBlock Text="Fangraphs" Margin="0,-30" FontSize="20" Foreground="White"/>
<Button x:Name="Add" Margin="0,30" Height="35" Width="125" Content="Add Player" HorizontalAlignment="Right" Click="Add_Click" />
</StackPanel>
</Grid>
<Grid Grid.Column="3">
<Grid.RowDefinitions>
<RowDefinition Height="16*" />
<RowDefinition Height="80*" />
<RowDefinition Height="4*" />
</Grid.RowDefinitions>
<DockPanel Grid.Row="1">
<ScrollViewer VerticalScrollBarVisibility="Auto">
<WrapPanel x:Name="PlayerCardList" Orientation="Horizontal" VerticalAlignment="Top" />
</ScrollViewer>
</DockPanel>
</Grid>
</Grid>
</Window>
MainWindow.xaml.cs:
using BaseballLinker.Objects;
using BaseballLinker.User_Controls;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
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 System.Xml.Serialization;
namespace BaseballLinker
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
InitializeComponent();
loadPlayerList();
populatePanel();
this.Name.Focus();
}
private ObservableCollection<Player> playerList;
public ObservableCollection<Player> PlayerList
{
get { return playerList; }
set
{
OnPropertyChanged("playerList");
}
}
protected virtual void OnPropertyChanged(string propName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void Add_Click(object sender, RoutedEventArgs e)
{
if (Name.Text != "")
{
var playerCard = new PlayerCard(Name.Text, MLB.Text, MiLB.Text, ESPN.Text, BRef.Text, FG.Text);
PlayerCardList.Children.Add(playerCard);
//Add Player Object To PlayerList
Player p = new Player { name = Name.Text,
mlb = MLB.Text,
milb = MiLB.Text,
espn = ESPN.Text,
baseballref = BRef.Text,
fangraphs = FG.Text };
playerList.Add(p);
App.SerializeToXML(playerList);
clearFields();
}
else
{
MessageBox.Show("A name is required.");
Name.Focus();
}
}
private void clearFields()
{
Name.Clear();
MLB.Clear();
MiLB.Clear();
ESPN.Clear();
BRef.Clear();
FG.Clear();
Name.Focus();
}
public void loadPlayerList()
{
try
{
playerList = App.DeserializeFromXML();
}
catch (Exception e)
{
playerList = new ObservableCollection<Player>();
}
}
public void populatePanel()
{
foreach (Player p in playerList)
{
var playerCard = new PlayerCard(p.name, p.mlb, p.milb, p.espn, p.baseballref, p.fangraphs);
PlayerCardList.Children.Add(playerCard);
}
}
private void Name_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
if (e.KeyboardDevice.IsKeyDown(Key.Tab))
((TextBox)sender).SelectAll();
}
private void MLB_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
if (e.KeyboardDevice.IsKeyDown(Key.Tab))
((TextBox)sender).SelectAll();
}
private void MiLB_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
if (e.KeyboardDevice.IsKeyDown(Key.Tab))
((TextBox)sender).SelectAll();
}
private void ESPN_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
if (e.KeyboardDevice.IsKeyDown(Key.Tab))
((TextBox)sender).SelectAll();
}
private void BRef_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
if (e.KeyboardDevice.IsKeyDown(Key.Tab))
((TextBox)sender).SelectAll();
}
private void FG_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
if (e.KeyboardDevice.IsKeyDown(Key.Tab))
((TextBox)sender).SelectAll();
}
}
}
PlayerCard.xaml(用户控制)
<UserControl x:Class="BaseballLinker.User_Controls.PlayerCard"
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"
mc:Ignorable="d"
d:DesignHeight="225" d:DesignWidth="200" Height="225" Width="200">
<Border BorderBrush="Black" BorderThickness="3" Margin="5">
<Grid Background="LightGray">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="5*" />
<ColumnDefinition Width="90*" />
<ColumnDefinition Width="5*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="5*" />
<RowDefinition Height="31" />
<RowDefinition Height="18*" />
<RowDefinition Height="18*" />
<RowDefinition Height="18*" />
<RowDefinition Height="10*" />
</Grid.RowDefinitions>
<Button x:Name="DeleteButton" Grid.Column="1" Width="25" Content="X" HorizontalAlignment="Right" FontSize="5" Click="DeleteButton_Click" />
<TextBlock x:Name="PlayerName" Grid.Column="1" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Bottom" />
<Button x:Name="MLBButton" Content="MLB" IsEnabled="False" HorizontalAlignment="Left"
Grid.Column="1" Grid.Row="2" Width="75" Height="25" VerticalAlignment="Bottom" Click="MLBButton_Click" />
<Button x:Name="MiLBButton" Content="MiLB" IsEnabled="False" HorizontalAlignment="Right"
Grid.Column="1" Grid.Row="2" Width="75" Height="25" VerticalAlignment="Bottom" Click="MiLBButton_Click" />
<Button x:Name="ESPNButton" Content="ESPN" IsEnabled="False" HorizontalAlignment="Left"
Grid.Column="1" Grid.Row="3" Width="75" Height="25" VerticalAlignment="Bottom" Click="ESPNButton_Click" />
<Button x:Name="BRButton" Content="BRef" IsEnabled="False" HorizontalAlignment="Right"
Grid.Column="1" Grid.Row="3" Width="75" Height="25" VerticalAlignment="Bottom" Click="BRButton_Click" />
<Button x:Name="FGButton" Content="Fangraphs" IsEnabled="False" HorizontalAlignment="Center"
Grid.Column="1" Grid.Row="4" Width="75" Height="25" VerticalAlignment="Bottom" Click="FGButton_Click" />
</Grid>
</Border>
</UserControl>
PlayerCard.xaml.cs
using System;
using System.Collections.Generic;
using System.Diagnostics;
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;
namespace BaseballLinker.User_Controls
{
/// <summary>
/// Interaction logic for PlayerCard.xaml
/// </summary>
public partial class PlayerCard : UserControl
{
private string mlbURL;
private string milbURL;
private string espnURL;
private string baseballRefURL;
private string fangraphsURL;
public PlayerCard(string name, string mlb, string milb, string espn, string baseballRef, string fangraphs)
{
InitializeComponent();
PlayerName.Text = name;
//Check if URLs are empty & modify button state.
mlbURL = linkCheck(mlb, MLBButton);
milbURL = linkCheck(milb, MiLBButton);
espnURL = linkCheck(espn, ESPNButton);
baseballRefURL = linkCheck(baseballRef, BRButton);
fangraphsURL = linkCheck(fangraphs, FGButton);
}
private string linkCheck(string site, UIElement button)
{
if (site != "")
{
button.IsEnabled = true;
}
return site;
}
private void ESPNButton_Click(object sender, RoutedEventArgs e)
{
try
{
Process.Start(espnURL);
}
catch(Exception ex)
{
MessageBox.Show("Invalid URL");
}
}
private void MLBButton_Click(object sender, RoutedEventArgs e)
{
try
{
Process.Start(mlbURL);
}
catch (Exception ex)
{
MessageBox.Show("Invalid URL");
}
}
private void MiLBButton_Click(object sender, RoutedEventArgs e)
{
try
{
Process.Start(milbURL);
}
catch (Exception ex)
{
MessageBox.Show("Invalid URL");
}
}
private void BRButton_Click(object sender, RoutedEventArgs e)
{
try
{
Process.Start(baseballRefURL);
}
catch (Exception ex)
{
MessageBox.Show("Invalid URL");
}
}
private void FGButton_Click(object sender, RoutedEventArgs e)
{
try
{
Process.Start(fangraphsURL);
}
catch (Exception ex)
{
MessageBox.Show("Invalid URL");
}
}
private void DeleteButton_Click(object sender, RoutedEventArgs e)
{
((Panel)this.Parent).Children.Remove(this);
}
}
}