我制作了一个带有一个图像(HUE颜色)和一个滑块(亮度等级)的颜色选择器。
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.8*"/>
<ColumnDefinition Width="0.2*"/>
</Grid.ColumnDefinitions>
<Border x:Name="borderColorChart" Grid.Column="0">
<Grid>
<Image Stretch="Fill" Source="Assets/colorChart.PNG" MouseDown="Image_MouseDown" MouseMove="Image_MouseMove"/>
<Ellipse x:Name="colorMarker" Width="5" Height="5" StrokeThickness="1" Stroke="#FF0B0B0B"/>
</Grid>
</Border>
<Border x:Name="brightnessSliderBorder" Background="{DynamicResource BrightnessGradient}" Grid.Column="1">
<Grid>
<Slider x:Name="brightnessSlider" Orientation="Vertical" IsMoveToPointEnabled="True" Focusable="False" Minimum="0.0" Maximum="1.0" Style="{DynamicResource SliderStyle}" />
</Grid>
</Border>
</Grid>
这个颜色选择器包含在Popup中,当我点击一个切换按钮时,它会打开:
<ToggleButton x:Name="SelectColorChannel1" Grid.Row="0" Background="{Binding SelectedCurrentColor, ElementName=Channel1Color}">
<Popup IsOpen="{Binding IsChecked, ElementName=SelectColorChannel1}" StaysOpen="True">
<CMiX:ColorPicker x:Name="Channel1Color"/>
</Popup>
</ToggleButton>
以下是mousemove的代码:
private void Image_MouseDown(object sender, MouseButtonEventArgs e)
{
var cb = new CroppedBitmap((BitmapSource)(((Image)e.Source).Source), new Int32Rect((int)Mouse.GetPosition(e.Source as Image).X, (int)Mouse.GetPosition(e.Source as Image).Y, 1, 1));
_pixels = new byte[4];
cb.CopyPixels(_pixels, 4, 0);
UpdateCurrentColor();
UpdateMarkerPosition();
UpdateSlider();
}
private void Image_MouseMove(object sender, MouseEventArgs e)
{
if (Mouse.LeftButton == MouseButtonState.Pressed)
{
if (e.Source.GetType().Equals(typeof(Image)))
{
var cb = new CroppedBitmap((BitmapSource)(((Image)e.Source).Source), new Int32Rect((int)Mouse.GetPosition(e.Source as Image).X, (int)Mouse.GetPosition(e.Source as Image).Y, 1, 1));
_pixels = new byte[4];
cb.CopyPixels(_pixels, 4, 0);
UpdateMarkerPosition();
UpdateCurrentColor();
Mouse.Synchronize();
UpdateSlider();
}
}
}
这是更新标记位置的功能
private void UpdateMarkerPosition()
{
_markerTransform.X = Mouse.GetPosition(borderColorChart).X - (borderColorChart.ActualWidth / 2);
_markerTransform.Y = Mouse.GetPosition(borderColorChart).Y - (borderColorChart.ActualHeight / 2);
}
问题是,我无法在图像上滑动标记,我只能点击&#34;一旦移动它,如果颜色选择器包含在ContextMenu中,这个问题就不会发生。但我需要弹出窗口,因此它在图像上和使用滑块时找到正确的颜色时保持打开状态。
任何提示?谢谢
编辑1 --- 我已经做了一些测试,现在据我所知,当我使用弹出窗口时,MouseMove上的函数UpdateMarkerPosition()无效,但是如果我使用了一个上下文菜单,它正在工作...仍然,UpdateMarkerPosition()正在处理MouseDown
编辑2 --- 好的,现在我确切地知道这个条件:
if (Mouse.LeftButton == MouseButtonState.Pressed)
如果我使用wpf Popup ,永远不会成立
答案 0 :(得分:0)
从内取出
Popup
控件。并将其ToggleButton
属性设置为Placement
,将其单独放在外面。