[英]C# Wpf drawing with Combobox
我需要在組合框中進行繪圖的幫助。 我想選一個顏色組合框。 我在互聯網上找到了一些東西,但是都沒有用。 到目前為止,我有這個:
private void MyComb_DrawItem(object sender, DrawItemEventArgs e)
{
Graphics g = e.Graphics;
System.Drawing.Rectangle rect = e.Bounds;
ColorConverter converter = new ColorConverter();
if (e.Index >= 0)
{
string n = ((ComboBox)sender).Items[e.Index].ToString();
System.Drawing.Color c = (System.Drawing.Color)converter.ConvertFromString(n);
SolidBrush b = new SolidBrush(c);
g.FillRectangle(b, rect.X + 110, rect.Y + 5,
rect.Width - 10, rect.Height - 10);
}
}
這是我的drawItem方法
<Grid>
<ComboBox x:Name="MyComb" HorizontalAlignment="Left" Margin="66,81,0,0" VerticalAlignment="Top" Width="120" />
</Grid>
這是組合框的定義
Type colorType = typeof(System.Drawing.Color);
PropertyInfo[] propInfoList = colorType.GetProperties(BindingFlags.Static |
BindingFlags.DeclaredOnly | BindingFlags.Public);
foreach (PropertyInfo c in propInfoList)
{
MyComb.Items.Add(c.Name);
}
在這里,我用顏色名稱填充組合框,然后根據顏色名稱用實色填充combox。.但是我的繪制項目方法從未調用過。 我試圖創建一些DrawItem處理程序,但是,我的組合框沒有這樣的東西。。。然后,我讀了一些有關設置組合框的DrawMode屬性的信息,但是我的組合框根本沒有那種屬性。 v.4.6.1誰能告訴我,我想念什么?
非常感謝你
您遇到的最大問題是,即使您使用的是WPF API,也要嘗試使用為Winforms API編寫的代碼示例。 為了將來參考,您確實需要更加謹慎地標識在網上找到的教程和其他資源的上下文,以確保它們確實適用於您的方案。
碰巧的是,我們在堆棧溢出方面已經有許多相關問題:
WPF ComboBox作為System.Windows.Media.Colors >
WPF-將ComboBox項綁定到其值的前景
由組合框制成的非常簡單的顏色選擇器
這些都可能對您有用,但都是基於此問題的答案:
最初只用於顯示顏色名稱 ,因此使用XAML中的<ObjectDataProvider/>
元素進行了捷徑。 這導致需要在其他問題中使用轉換器,以將string
值或PropertyInfo
實例轉換為適當的顏色或畫筆。
實際上,如果您的代碼已經被編寫為使用某種類型的MVVM方法,尤其是因為您已經編寫了代碼以從Colors
類型中檢索顏色值,或者至少嘗試這樣做(您的問題之一,代碼是您使用的是Winforms Color
類型而不是WPF Colors
類型……同樣,在Winforms中可以正常工作,但是WPF API更嚴格地遵循Code Analysis / FxCop規則,並且命名的顏色為Colors
類型),堅持這樣做並提供可以綁定的直接視圖模型數據結構是很有意義的。
在這種方法中,您沒有在XAML中提供聲明性的實現,而是描述了每個項目的外觀,而不是提供項目圖的過程實現。
這是一個例子……
首先,一些簡單的視圖模型數據結構:
// Very simple container class
class ColorViewModel
{
public Brush Brush { get; }
public string Name { get; }
public ColorViewModel(Brush brush, string name)
{
Brush = brush;
Name = name;
}
}
// Main view model, acts as the data context for the window
class MainViewModel : INotifyPropertyChanged
{
public IReadOnlyList<ColorViewModel> Colors { get; }
private Brush _selectedColor;
public Brush SelectedColor
{
get { return _selectedColor; }
set { _UpdateField(ref _selectedColor, value); }
}
public MainViewModel()
{
Colors = typeof(Colors).GetProperties(BindingFlags.Static | BindingFlags.Public)
.Select(p => new ColorViewModel(new SolidColorBrush((Color)p.GetValue(null)), p.Name))
.ToList().AsReadOnly();
}
public event PropertyChangedEventHandler PropertyChanged;
private void _UpdateField<T>(ref T field, T newValue, [CallerMemberName] string propertyName = null)
{
if (!EqualityComparer<T>.Default.Equals(field, newValue))
{
field = newValue;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
有了這些,XAML就很簡單了:
<Window x:Class="TestSO47850587ColorComboBox.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:l="clr-namespace:TestSO47850587ColorComboBox"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<l:MainViewModel/>
</Window.DataContext>
<StackPanel>
<ComboBox Width="100" ItemsSource="{Binding Colors}"
HorizontalAlignment="Left" Grid.IsSharedSizeScope="True"
SelectedValuePath="Brush" SelectedValue="{Binding SelectedColor}">
<ComboBox.ItemTemplate>
<DataTemplate DataType="{x:Type l:ColorViewModel}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="ComboBoxItem"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Name}" Background="{Binding Brush}" HorizontalAlignment="Stretch"/>
</Grid>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<Rectangle HorizontalAlignment="Stretch" Height="24" Fill="{Binding SelectedColor}"/>
</StackPanel>
</Window>
上面以實際顏色為背景顯示顏色名稱。 如果您只想要一個矩形,則可以將<TextBlock/>
元素替換為<Rectangle/>
元素,並綁定到其Fill
屬性。 當然,您可以實現其他視覺效果,例如帶有邊距的矩形。 只需根據需要配置數據模板即可。
這里的要點是,您應該采用定義良好WPF編程的數據綁定方法,並且絕對不應該將Winforms代碼示例誤認為WPF。 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.