<UserControl xmlns:my="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" x:Class="Silverlight.Common.uc_DataGrid"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400" >
<Grid x:Name="LayoutRoot" Background="White">
<my:DataGrid Height="300" HorizontalAlignment="Left" Name="dgList" Width="600">
<my:DataGrid.Columns>
<my:DataGridTextColumn Header="ID" Binding="{Binding ID}" Width="100"></my:DataGridTextColumn>
<my:DataGridTemplateColumn Header="分类">
<my:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Width="{Binding Width}" Height="{Binding Height}" Source="{Binding Source}" MouseLeftButtonDown="Image_MouseLeftButtonDown"></Image>
</StackPanel>
</DataTemplate>
</my:DataGridTemplateColumn.CellTemplate>
</my:DataGridTemplateColumn>
</my:DataGrid.Columns>
</my:DataGrid>
</Grid>
</UserControl>
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400" >
<Grid x:Name="LayoutRoot" Background="White">
<my:DataGrid Height="300" HorizontalAlignment="Left" Name="dgList" Width="600">
<my:DataGrid.Columns>
<my:DataGridTextColumn Header="ID" Binding="{Binding ID}" Width="100"></my:DataGridTextColumn>
<my:DataGridTemplateColumn Header="分类">
<my:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Width="{Binding Width}" Height="{Binding Height}" Source="{Binding Source}" MouseLeftButtonDown="Image_MouseLeftButtonDown"></Image>
</StackPanel>
</DataTemplate>
</my:DataGridTemplateColumn.CellTemplate>
</my:DataGridTemplateColumn>
</my:DataGrid.Columns>
</my:DataGrid>
</Grid>
</UserControl>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace Silverlight.Common
{
public partial class uc_DataGrid : UserControl
{
public uc_DataGrid()
{
InitializeComponent();
Bind();
}
List<Model> list = new List<Model>();
public class Model
{
public int ID { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public string Source { get; set; }
public Model() { }
public Model(int _ID, int _Width, int _Height, string _Source)
{
ID = _ID;
Width = _Width;
Height = _Height;
Source = _Source;
}
}
private void Bind()
{
list.Clear();
list.Add(new Model(1, 100, 50, "icon/btnNew.jpg"));
list.Add(new Model(2, 50, 100, "icon/btnOpen.jpg"));
dgList.ItemsSource = list;
}
private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Model model = dgList.SelectedItem as Model;
//MessageBox.Show(model.ID.ToString());
if (model.ID == 1)
{
BindNew();
}
if (model.ID == 2)
{
BindNew2();
}
}
private void BindNew()
{
list.Clear();
list.Add(new Model(1, 100, 50, "icon/btnNew.jpg"));
list.Add(new Model(2, 50, 100, "icon/btnOpen.jpg"));
list.Add(new Model(3, 50, 100, "icon/btnOpen.jpg"));
dgList.ItemsSource = list;
}
private void BindNew2()
{
list.Clear();
list.Add(new Model(1, 100, 50, "icon/btnNew.jpg"));
list.Add(new Model(2, 50, 100, "icon/btnOpen.jpg"));
dgList.ItemsSource = list;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace Silverlight.Common
{
public partial class uc_DataGrid : UserControl
{
public uc_DataGrid()
{
InitializeComponent();
Bind();
}
List<Model> list = new List<Model>();
public class Model
{
public int ID { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public string Source { get; set; }
public Model() { }
public Model(int _ID, int _Width, int _Height, string _Source)
{
ID = _ID;
Width = _Width;
Height = _Height;
Source = _Source;
}
}
private void Bind()
{
list.Clear();
list.Add(new Model(1, 100, 50, "icon/btnNew.jpg"));
list.Add(new Model(2, 50, 100, "icon/btnOpen.jpg"));
dgList.ItemsSource = list;
}
private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Model model = dgList.SelectedItem as Model;
//MessageBox.Show(model.ID.ToString());
if (model.ID == 1)
{
BindNew();
}
if (model.ID == 2)
{
BindNew2();
}
}
private void BindNew()
{
list.Clear();
list.Add(new Model(1, 100, 50, "icon/btnNew.jpg"));
list.Add(new Model(2, 50, 100, "icon/btnOpen.jpg"));
list.Add(new Model(3, 50, 100, "icon/btnOpen.jpg"));
dgList.ItemsSource = list;
}
private void BindNew2()
{
list.Clear();
list.Add(new Model(1, 100, 50, "icon/btnNew.jpg"));
list.Add(new Model(2, 50, 100, "icon/btnOpen.jpg"));
dgList.ItemsSource = list;
}
}
}
说明: 示例所需图片自己添加