嗨我有一個讓我的WPF顏色選擇器工作的問題。我認爲它是因爲我以錯誤的方式起訴Databinding,但我不確定,因爲我沒有完全理解它。WPF Databinding製作顏色選擇器
我想要的是一個下拉列表,它包含我列表中的顏色(包含如顯示的實際顏色不僅僅是文本)。
繼承人我的代碼:
WPF:
<ComboBox Name="cb_farbe" Text="farbe" HorizontalContentAlignment="Center" IsEditable="True" Grid.Row="7" Grid.Column="1" VerticalAlignment="Center" Grid.ColumnSpan="2" Loaded="CbFarbe">
<ComboBox.ItemTemplate>
<DataTemplate>
<Rectangle Grid.Column="0" Margin="5, 10" Fill="{Binding}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
C#:
private void CbFarbe(object sender, RoutedEventArgs e)
{
List<Color> colors = new List<Color>
{
Color.Blue,
Color.Green,
Color.LightBlue,
Color.Black,
Color.White,
Color.Gray
};
var comboBox = sender as ComboBox;
comboBox.ItemsSource = colors;
comboBox.SelectedIndex = 1;
this.DataContext = colors;
}
嘗試'列表'並用'Brushes.Blue'填充它等。除此之外,Rectange沒有設置大小。 –
Clemens
@Clemens這樣我只是得到字符串「System.Drawing.SolidBrush在我的默認選定的索引和所有其他選項都是空的」 – Samuel
我的意思是WPF畫筆,而不是WinForms畫筆,即System.Windows.Media.Brushes。一般來說,你不應該在WPF應用程序中使用WinForms類。刪除System.Drawing程序集引用。 – Clemens