【问题标题】:UWP ColorPickerButton in UWP DataGrid cell won't be selectedUWP DataGrid 单元格中的 UWP ColorPickerButton 不会被选中
【发布时间】:2021-10-19 01:44:02
【问题描述】:

我从这里使用DataGrid 和ColorPickerButton

xmlns:toolkit="using:Microsoft.Toolkit.Uwp.UI.Controls

当我点击ColorPickerButton 时,它保持打开状态并且颜色属性没有改变。

如何解决? 谢谢!

XAML

 <toolkit:DataGrid    
                    x:Name="dataGridLayers"
                    Margin="0,0,0,0"
                    VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
                    HorizontalScrollBarVisibility="Visible"
                    VerticalScrollBarVisibility="Visible"
                    AlternatingRowBackground="Transparent" 
                    AreRowDetailsFrozen="False"
                    AreRowGroupHeadersFrozen="True"
                    AutoGenerateColumns="False"
                    CanUserSortColumns="False"
                    CanUserReorderColumns="True"
                    RowGroupHeaderPropertyNameAlternative=""
                    CanUserResizeColumns="True"
                    ColumnHeaderHeight="32"
                    MaxColumnWidth="400"
                    FrozenColumnCount="0"
                    GridLinesVisibility="None"
                    HeadersVisibility="Column"
                    IsReadOnly="False" 
                    Height="400"
                    RowDetailsVisibilityMode="Collapsed"
                    SelectionMode="Single">
                                <toolkit:DataGrid.Columns>
                                    <toolkit:DataGridTextColumn Header="Name" Binding="{Binding LayerName}"  Tag="LayerName" IsReadOnly="True" />
                                    <toolkit:DataGridCheckBoxColumn IsThreeState="False"  Header="Включить в карту" Binding="{Binding LayerVisibility,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"  Tag="LayerVisibility"  />
                                    <toolkit:DataGridTextColumn Header="Stroke" Binding="{Binding LayerStroke, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"   Tag="LayerStroke" />
                                    <toolkit:DataGridTemplateColumn  Header="Color"  Tag="LayerColor">
                                        <toolkit:DataGridTemplateColumn.CellTemplate>
                                            <DataTemplate >
                                                <toolkit:ColorPickerButton SelectedColor="{Binding LayerColor, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                                            </DataTemplate>
                                        </toolkit:DataGridTemplateColumn.CellTemplate>
                                    </toolkit:DataGridTemplateColumn>
                                </toolkit:DataGrid.Columns>
                            </toolkit:DataGrid>
                     

C#

 public abstract class EtherMapLayerBase : INotifyPropertyChanged
    {
        public Windows.UI.Color _layerColor { get; set; }
        public Windows.UI.Color LayerColor
        {
            get
            {
                return _layerColor;
            }

            set
            {
                if (_layerColor != value)
                {
                    _layerColor = value;
                    OnPropertyChanged();
                }
            }
        }


        public int _layerStroke { get; set; }
        public int LayerStroke
        {
            get
            {
                return _layerStroke;
            }

            set
            {
                if (_layerStroke != value)
                {
                    _layerStroke = value;
                    OnPropertyChanged();
                }
            }
        }


        public string _layerName { get; set; }
        public string LayerName
        {
            get
            {
                return _layerName;
            }

            set
            {
                if (_layerName != value)
                {
                    _layerName = value;
                    OnPropertyChanged();
                }
            }
        }


        public bool _layerVisibility { get; set; }
        public bool LayerVisibility
        {
            get
            {
                return _layerVisibility;
            }

            set
            {
                if (_layerVisibility != value)
                {
                    _layerVisibility = value;
                    OnPropertyChanged();
                }
            }
        }


        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] String propertyName = null)
        {
            if (object.Equals(storage, value))
            {
                return false;
            }
            else
            {
                storage = value;
                OnPropertyChanged(propertyName);
                return true;
            }
        }
    }

    public class EtherMapLayerView : EtherMapLayerBase
    {
        public EtherMapLayerView()
        {
            LayerStroke = 1;
            LayerColor = Colors.Black; // using Windows.UI;
            LayerVisibility = true;
        }
    }

更新 #1 好像是个bug……

因此,当您在第一个 TAB 中选择颜色时,没有任何反应,但如果在其他 TAB 上执行相同操作,则可以成功选择颜色。

【问题讨论】:

    标签: c# uwp datagrid color-picker


    【解决方案1】:

    当我点击 ColorPickerButton 时,它保持打开状态并且颜色属性没有改变。

    问题是你将基色设置为BlackLayerColor = Colors.Black,这样会导致无法选择色轮。

    请尝试将默认颜色设置为Transparent,它将按预期工作。

    LayerColor = Colors.Transparent
    

    【讨论】:

    • 非常感谢您!嘿,我仍然对toolkit:DataGridCheckBoxColumn 有类似的问题。为了取消它,我必须点击 3 次。它需要一些额外的点击才能以某种方式选择控件。你介意帮我吗?
    • 当然,示例方法将复选框与 bool 属性绑定,并使用额外的按钮将这些 bool 属性设置为 false。
    • 谢谢你!我按照你说的做了:&lt;controls:DataGridTemplateColumn.CellTemplate&gt; &lt;DataTemplate&gt;&lt;StackPanel Orientation="Horizontal"&gt;&lt;CheckBox IsChecked="{Binding MyCheckField}" /&gt;
    猜你喜欢
    • 1970-01-01
    • 2022-01-15
    • 2021-08-10
    • 1970-01-01
    • 2019-07-23
    • 1970-01-01
    • 2022-11-05
    • 1970-01-01
    • 2021-02-09
    相关资源
    最近更新 更多