Home > Blockchain >  Is there a way to select the contents of COMBOBOX for copy/paste?
Is there a way to select the contents of COMBOBOX for copy/paste?

Time:11-16

Problem: So my application has 2 options, load and update. When I want to update, I also want to copy the content (like Name/UID), but I can't. During update I set "isEnabled=False" (by binding it to a variable) it wont let me copy the content.

I tried doing "isReadOnly=True"(removing the "isEnabled" property), its allowing me to copy, but the DropDown is still working, which will allow me or anyone to change certain values,(like gender, UID) to change during update.

Goal: I want to be able to copy the content of the combobox but not letting anyone change its value.

OR

is there a way to disable the dropdown feature so that "isReadOnly=True" would do the trick.

CodePudding user response:

If isReadOnly=True is doing what you want, then I will just use a converter to disable the dropdown.

In MainWindow.xaml:

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApp1">
    <Window.Resources>
        <local:CBMaxDropDownHeightConverter x:Key="CBMaxDropDownHeightConverter" />
    </Window.Resources>
    <Grid>
        <ComboBox MaxDropDownHeight="{Binding RelativeSource={RelativeSource Self}, Path=IsReadOnly, Converter={StaticResource CBMaxDropDownHeightConverter}}" />
    </Grid>
</Window>

Then in CBMaxDropDownHeightConverter.cs

using System;
using System.Windows.Data;

namespace WpfApp1
{
    public class CBMaxDropDownHeightConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (System.Convert.ToBoolean(value) == true)
            {
                return "0";
            }

            return "Auto";
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
}
  • Related