【发布时间】:2013-12-05 14:07:11
【问题描述】:
我正在尝试制作一个继承自 ComboBox 的自定义颜色选择器。
这是截图。
如您所见,它只是一个非常普通的颜色选择器界面。
到目前为止,除了我在截图中写的问题之外,几乎一切都很好。
我可以毫无问题地拖动频谱滑块、RGBA 滑块并单击这两个按钮。
如果我单击 ComboBoxItem 内的空白区域,弹出窗口就会消失,这也是我需要的行为并且它有效。
这里是画布区域的代码。
XAML
<Canvas x:Name="colorPlatte" Width="175" Height="150" Grid.Row="0" Grid.Column="0" Margin="4" MouseLeftButtonDown="colorPlatte_MouseLeftButtonDown" MouseLeftButtonUp="colorPlatte_MouseLeftButtonUp" MouseMove="colorPlatte_MouseMove">
<Rectangle x:Name="ColorShadingRectangle"
Height="{Binding ElementName=colorPlatte, Path=Height}"
Width="{Binding ElementName=colorPlatte, Path=Width}"
Fill="{Binding ElementName=sliderSpectrum, Path=SelectedColor, Converter={StaticResource ColorToSolidBrush}}"/>
<Rectangle x:Name="WhiteGradient" Width="{Binding ElementName=colorPlatte,Path=Width}" Height="{Binding ElementName=colorPlatte,Path=Height}">
<Rectangle.Fill>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
<GradientStop Offset="0" Color="#ffffffff" />
<GradientStop Offset="1" Color="Transparent" />
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Rectangle x:Name="BlackGradient" Width="{Binding ElementName=colorPlatte,Path=Width}" Height="{Binding ElementName=colorPlatte,Path=Height}">
<Rectangle.Fill>
<LinearGradientBrush StartPoint="0,1" EndPoint="0, 0">
<GradientStop Offset="0" Color="#ff000000" />
<GradientStop Offset="1" Color="#00000000" />
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Canvas x:Name="colorSelector" Width="12" Height="12" IsHitTestVisible="False">
<Ellipse Width="12" Height="12" StrokeThickness="3" Stroke="#FFFFFFFF" IsHitTestVisible="False" />
<Ellipse Width="12" Height="12" StrokeThickness="1" Stroke="#FF000000" IsHitTestVisible="False" />
</Canvas>
</Canvas>
与画布相关的代码隐藏
private void colorPlatte_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Point p = e.GetPosition(colorPlatte);
UpdateColorSelectorPosColor(p);
colorPlatte.CaptureMouse();
}
private void colorPlatte_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
Point p = e.GetPosition(colorPlatte);
UpdateColorSelectorPosColor(p);
Mouse.Synchronize();
}
}
private void colorPlatte_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
colorPlatte.ReleaseMouseCapture();
}
那么,我应该如何防止用户拖动颜色选择器后弹出关闭?
谢谢。
编辑:12/6
抱歉没有把我的问题描述清楚,错过了,之前的截图是直接在设计模式下截取的,可能会忽略太多。
我不应该说从ComboBox继承,ColorPicker实际上是一个UserControl,我将它放在baseComboBoxItem(从ComboBoxItem继承)中,在baseComboBox(从ComboBox继承)下
当用户点击 ComboBox 时,只有一个弹出框(ColorPicker)供用户选择颜色。
但是如果用户拖动颜色选择器(请参考之前的截图),它会导致组合框弹出(下拉?)自动关闭。
我的问题是我应该如何在用户拖动颜色选择器后(用户触发 MouseLeftButtonUp 事件后)保持弹出窗口打开?
【问题讨论】:
-
这不是
ComboBox。如果您不需要它提供的功能,请不要从ComboBox继承。ComboBox是ItemsControl。我在你的 UI 中看不到任何项目。 -
请查看我的问题更新
-
哇! +1 有关图片和内容的广泛问题。不错!