//xaml
<Window x:Class="WpfApp109.MainWindow"
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"
xmlns:local="clr-namespace:WpfApp109"
WindowState="Maximized"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Viewport3D>
<Viewport3D.Camera>
<PerspectiveCamera Position="3,3,4"
LookDirection="-1,-1,-1"
FieldOfView="60"/>
</Viewport3D.Camera>
<Viewport3D.Children>
<ModelVisual3D>
<ModelVisual3D.Content>
<DirectionalLight Direction="-0.3,-0.4,-0.5"/>
</ModelVisual3D.Content>
</ModelVisual3D>
<ModelUIElement3D
MouseLeftButtonDown="ModelUIElement3D_MouseLeftButtonDown"
MouseRightButtonDown="ModelUIElement3D_MouseRightButtonDown" >
<ModelUIElement3D.Model>
<GeometryModel3D>
<GeometryModel3D.Material>
<DiffuseMaterial>
<DiffuseMaterial.Brush>
<SolidColorBrush Color="Purple" x:Name="cubeBrush"/>
</DiffuseMaterial.Brush>
</DiffuseMaterial>
</GeometryModel3D.Material>
<GeometryModel3D.Geometry>
<MeshGeometry3D
Positions="1,1,-1 1,-1,-1 -1,-1,-1 -1,1,-1 1,1,1 -1,1,1 -1,-1,1 1,-1,1
1,1,-1 1,1,1 1,-1,1 1,-1,-1 1,-1,-1 1,-1,1 -1,-1,1 -1,-1,-1
-1,-1,-1 -1,-1,1 -1,1,1 -1,1,-1 1,1,1 1,1,-1 -1,1,-1 -1,1,1"
TriangleIndices="0 1 2 0 2 3 4 5 6 4 6 7 8 9 10 8 10 11 12 13 14 12 14
15 16 17 18 16 18 19 20 21 22 20 22 23"
TextureCoordinates="0,1 0,0 1,0 1,1 1,1 -0,1 0,-0 1,0 1,1 -0,1 0,-0
1,0 1,0 1,1 -0,1 0,-0 -0,0 1,-0 1,1 0,1 1,-0 1,1
0,1 -0,0" />
</GeometryModel3D.Geometry>
</GeometryModel3D>
</ModelUIElement3D.Model>
</ModelUIElement3D>
</Viewport3D.Children>
</Viewport3D>
</Grid>
</Window>
//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;
namespace WpfApp109
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
static Random rnd;
List<Color> colorsList { get; set; }
int colorsCount { get; set; }
int colorIdx = 0;
public MainWindow()
{
InitializeComponent();
rnd = new Random();
colorsList = new List<Color>();
var pis = typeof(Colors).GetProperties();
if (pis != null && pis.Any())
{
int pisCount = pis.Count();
for (int i = 0; i < pisCount; i++)
{
Color cr = (Color)pis[i].GetValue(typeof(Colors));
colorsList.Add(cr);
}
colorsCount = colorsList.Count;
}
this.Title = colorIdx.ToString();
}
private void ModelUIElement3D_MouseDown(object sender, MouseButtonEventArgs e)
{
if (colorIdx >= colorsCount)
{
colorIdx = 0;
}
this.Title = colorIdx.ToString();
cubeBrush.Color = colorsList[colorIdx++];
//cubeBrush.Color=Color.FromRgb((byte)rnd.Next(),(byte)rnd.Next(), (byte)rnd.Next());
}
private void ModelUIElement3D_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
if (--colorIdx < 0)
{
colorIdx = colorsCount - 1;
}
cubeBrush.Color = colorsList[colorIdx];
this.Title = colorIdx.ToString();
}
private void ModelUIElement3D_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (colorIdx >= colorsCount)
{
colorIdx = 0;
}
this.Title = colorIdx.ToString();
cubeBrush.Color = colorsList[colorIdx++];
}
}
}