logo
down
shadow

WPF Hiding the First Item of an ItemsControl (Combobox,textbox,etc)


WPF Hiding the First Item of an ItemsControl (Combobox,textbox,etc)

By : nadeem Saif
Date : November 21 2020, 04:01 AM
Hope that helps You can declare a boolean property which will return false if Collection.Count() == 1 and use a boolean to visibility converter for your combo box
code :


Share : facebook icon twitter icon
How to access ItemsControl's ItemsSource element from the ItemsControl's Item's code behind?

How to access ItemsControl's ItemsSource element from the ItemsControl's Item's code behind?


By : user3131561
Date : March 29 2020, 07:55 AM
this one helps. What is important is the context in which you try to access that object. If you for example deal with an event inside the DataTemplate you can easily get the object from the DataContext of the sender (has to be a FrameworkElement), e.g. if i were to handle a button click:
code :
private void Button_Click(object sender, RoutedEventArgs e)
{
    var button = (FrameworkElement)sender;
    var employee = (Employee)button.DataContext;
    //...
}
WPF ComboBox with editable textbox as an item

WPF ComboBox with editable textbox as an item


By : user3363086
Date : March 29 2020, 07:55 AM
Does that help I'm looking to have a combo box with only two items: , Not quite sure what the problem is, have you tried this:
code :
<ComboBox>
    <ComboBoxItem>Other</ComboBoxItem>
    <TextBox>TextBox</TextBox>
</ComboBox>
<ComboBox>
    <ComboBoxItem>Normal Item</ComboBoxItem>
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="Other: " VerticalAlignment="Center"/>
        <TextBox>Enter text...</TextBox>
    </StackPanel>
</ComboBox>
Styling a WPF Combobox with a Textbox-Item

Styling a WPF Combobox with a Textbox-Item


By : dqthebt
Date : March 29 2020, 07:55 AM
This might help you You can achieve this by using a TextBox to be able to edit the text (like you've done) and a TextBlock to display the text when the item is selected.
You can show/hide the TextBlock/TextBox by binding the Visibility of them to the ComboBoxItem's IsSelected value and use a ValueConverter to convert the true/false value to Visible/Collapsed.
code :
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <ComboBox Name="myComboBox" Margin="0 2 0 2" SelectedIndex="0" Grid.Row="1">
        <ComboBoxItem>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="30"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Grid Grid.Column="1">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="16" MinHeight="16" MaxHeight="16" />
                        <RowDefinition Height="16" MinHeight="16" MaxHeight="16" />
                    </Grid.RowDefinitions>
                    <TextBlock Text="Item Titel 1" Grid.Row="0" FontWeight="Bold" />
                    <TextBlock Text="Item Beschreibung 1" Grid.Row="1" FontStyle="Italic" Foreground="#FF555454" />
                </Grid>
            </Grid>
        </ComboBoxItem>
        <ComboBoxItem Name="myComboBoxItem">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="30"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Grid Grid.Column="1">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="16" MinHeight="16" MaxHeight="16" />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <TextBlock Text="Item Titel 2" Grid.Row="0" FontWeight="Bold" />

                    <TextBox Grid.Row="1" Name="myTextBox" Text="c:\temp\test" Height="20" Visibility="{Binding ElementName=myComboBoxItem, Path=IsSelected, Converter={ValueConverter:BooleanToVisibilityConverter}}" />
                    <TextBlock Name="myTextBlock" Text="{Binding ElementName=myTextBox, Path=Text}" Grid.Row="1" FontStyle="Italic" Foreground="#FF555454" Visibility="{Binding ElementName=myComboBoxItem, Path=IsSelected, Converter={ValueConverter:BooleanToVisibilityConverter}, ConverterParameter=Inverted}" />
                </Grid>
            </Grid>
        </ComboBoxItem>
    </ComboBox>
</Grid>
public abstract class BaseConverter : MarkupExtension
{
    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }
}

public class BooleanToVisibilityConverter : BaseConverter, IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {

        if (parameter != null && parameter.ToString().Equals("Inverted"))
        {
            if ((bool)value)
                return Visibility.Visible;
            return Visibility.Collapsed;
        }
        if ((bool)value)
            return Visibility.Collapsed;
        return Visibility.Visible;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        try
        {
            if (parameter.ToString().Equals("Inverted"))
                return (Visibility)value != Visibility.Visible;
            return (Visibility)value == Visibility.Visible;
        }
        catch (Exception e)
        {
            // Error handling
            return false;
        }
    }
}
highlight combobox item if it's text starts with the text of the combobox's textbox

highlight combobox item if it's text starts with the text of the combobox's textbox


By : fabian sanchez
Date : March 29 2020, 07:55 AM
will help you I assume that the items in your ComboBox are just plain string values. You will have to change that and create a class to display each item. The reason for this is that you will need some bool 'flag' property that you can bind to a DataTrigger that will highlight your entries according to your requirement. So you could do this:
code :
public class CustomComboBoxItem : INotifyPropertyChanged
{
    public string Value { get; set; } // Implement INotifyPropertyChanged correctly...
    public bool IsHighlighted { get; set; } // ... here, unlike this example
}
public ObservableCollection<CustomComboBoxItem> Items { get; set; }
<ComboBox ItemsSource="{Binding Items}" ... />
<DataTemplate DataType="{x:Type YourXmlNamespacePrefix:CustomComboBoxItem}">
    <TextBlock Text="{Binding Value}">
        <TextBlock.Style>
            <Style>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsHighlighted}" Value="True">
                        <Setter Property="Background" Value="LightGreen" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </TextBlock.Style>
    </TextBlock>
</DataTemplate>
public ObservableCollection<CustomComboBoxItem> Items { get; set; }
public string InputValue 
{
    get { return inputValue; }
    set 
    {
        inputValue = value;
        NotifyPropertyChanged("Items");
        for (int i = 0; i < Items.Count; i++)
        {
            Items[i].IsHighlighted = Items[i].StartsWith(inputValue);
        }
    }
}

...

<ComboBox ItemsSource="{Binding Items}" Text="{Binding InputValue}" ... />
How to create ComboBox + TextBox in a ListView and add entered text as new item in ComboBox

How to create ComboBox + TextBox in a ListView and add entered text as new item in ComboBox


By : user3516577
Date : March 29 2020, 07:55 AM
will be helpful for those in need
Now the problem is I've set the converter class properties as global so when I enter text in one list item it reflect in other items as well.
code :
public class MyComboBox : ComboBox
{
    public MyComboBox()
    {
        this.Loaded += MyComboBox_Loaded;
    }

    private void MyComboBox_Loaded(object sender, RoutedEventArgs e)
    {
        TextSubmitted += MyComboBox_TextSubmitted;
    }

    private void MyComboBox_TextSubmitted(ComboBox sender, ComboBoxTextSubmittedEventArgs args)
    {
        var items = ItemsSource as ObservableCollection<UserRole>;
        if (items.Count > 0)
        {
            if (items.Any<UserRole>(s => s.name == args.Text))
            {
                return;
            }
            else
            {
                var newItem = new UserRole() { id = (items.Count + 1).ToString(), name = args.Text };
                items.Add(newItem);
            }

        }
    }
}
<Grid HorizontalAlignment="Center" VerticalAlignment="Center">
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>
    <ListView
        x:Name="DataListView"
        HorizontalAlignment="Center"
        VerticalAlignment="Center"
        ItemsSource="{x:Bind UserList}"
        >
        <ListView.ItemTemplate>
            <DataTemplate x:DataType="data:User">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Width="150" Text="{x:Bind username}" />
                    <TextBlock Width="150" Text="{x:Bind fisrt_name}" />
                    <TextBlock Width="150" Text="{x:Bind last_name}" />
                    <TextBlock Width="150" Text="{x:Bind user_role.name, Mode=OneWay}" />
                    <local:MyComboBox
                        Width="150"
                        DisplayMemberPath="name"
                        IsEditable="True"
                        IsTextSearchEnabled="True"
                        ItemsSource="{Binding ElementName=DataListView, Path=DataContext.UserRoleList, Mode=TwoWay}"
                        SelectedItem="{x:Bind user_role, Mode=TwoWay}"
                        />
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    <Button
        Name="submit"
        Grid.Row="1"
        Click="submit_Click"
        Content="click"
        />
</Grid>
Related Posts Related Posts :
  • Why does my WPF combobox displays Model name that is binded when selected?
  • WPF and powershell, need to know how to add style to a control element
  • Make images in grid same size
  • DataPicker Button influenced by a global styling on all Buttons
  • How to change WPF Combobox color to White when no items are selected?
  • Hiding popup on mouse click with Popup.StaysOpen in WPF
  • XAML WPF Vertical Scroll bar line by line instead of group by group when grouping
  • WPF <StatusBar> is not positioned at the bottom of the window
  • Multicolumn listview in WPF at design time
  • WPF DropShadow Disappears
  • WPF- Changing Tooltip background to Transparent
  • in wpf datagrid how to get the blank row on top?
  • Prism: Exception handling?
  • NLTK in IronPython from WPF
  • WPF: How can i scale to fit the content?
  • listbox dragdrop in wpf
  • Resizing Listbox Contents according to Listbox dimensions
  • WPF InvalidateMeasure not called during or after animation
  • How can I define how many spaces a TAB jumps in a XAML TextBox?
  • The calling thread must be STA, because many UI components require this
  • WPF Popup hiding problem
  • ModelVisual3D vs Model3DGroup
  • Composite Commands Not Working
  • WPF ListBox Scroll to end automatically
  • 'Default' text for templated combo box
  • MDI and WPF Ribbon
  • WPF FontSize style hangs / freezes / stops responding
  • WPF: Textbox width guidelines
  • WPF mediaelement
  • MVVM Passing data to dialog View Model
  • Embedding WinForms Graph in WPF Window
  • WPF: Focus in a Window and UserControl
  • How can I stretch bitmap in WPF without smoothing pixels
  • Changing DataTemplate TextBlock Property at Runtime
  • Image source using project resource (WPF)
  • WPF combobox colors
  • WPF: Window stays minimized even when setting WindowState explicitly
  • WPF: Drag/Drop to re-order grid and jiggle
  • Dynamically updating a WPF ListView after changes
  • WPF: How do I implement a stretch transform?
  • intercept RelativeSource FindAncestor
  • WPF Listbox wont scroll Vertical
  • WPF window on closing
  • WPF UserControl is not drawn when overriding MeasureOverride and ArrangeOverride
  • Master/Detail UI Best Practices?
  • shadow
    Privacy Policy - Terms - Contact Us © bighow.org