Solved

ListboxItem Layout based on listbox width/select style

Posted on 2010-09-14
5
614 Views
Last Modified: 2013-11-12
Hi All,

is it possible to set listboxitem layout based on listbox layout? attached you find an example of what i'd like to reach

listbox with 3 label:
1 select char: I'd like do not have the normal select style, no background color change
2 text
3 text right aligned

is it possible? wanted layout
0
Comment
Question by:Lukino2000
  • 4
5 Comments
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 33674443
see sample xaml and code
<Grid>

        <Grid.Resources>
            <local:MyConverter x:Key="temp"/>
            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="IsSelected" Value="{Binding IsSelected}"/>
            </Style>
        </Grid.Resources>
        <ListBox x:Name="temp" SelectedItem="{Binding SelectedItem}">
            <ListBox.Style>
                <Style TargetType="{x:Type ListBox}">
                    <Style.Resources>
                        <!-- Background of selected item when focussed -->
                        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="White" />
                        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
                    </Style.Resources>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="ListBox">
                                <Border CornerRadius="5" Background="{TemplateBinding ListBox.Background}">
                                    <ScrollViewer HorizontalScrollBarVisibility="Auto">
                                        <StackPanel Orientation="Vertical"
                       VerticalAlignment="Center"
                       HorizontalAlignment="Center"
                       IsItemsHost="True"/>
                                    </ScrollViewer>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ListBox.Style>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Width="25" Text=">" Visibility="{Binding Mode=TwoWay, Path=IsSelected,Converter={StaticResource temp}}"/>
                        <TextBlock Width="75" Text="{Binding Name}"/>
                        <TextBlock Width="75" HorizontalAlignment="Right" Text="{Binding Value}"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>

        </ListBox>

    </Grid>

Open in new window

0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 33674447
code behind
temp.DataContext = this;

            temp.ItemsSource = new List<object>()
            {
                new Item{ Name ="4PB" ,Value =12,IsSelected = false},
                new Item{ Name ="4B" ,Value =19,IsSelected = false },
                new Item {Name ="M12B" ,Value =3,IsSelected = false }
            };



public class Item :INotifyPropertyChanged
    {

        private bool _isSelected;
        public bool IsSelected
        {
            get
            {
                return _isSelected;
            }
            set
            {
                _isSelected = value;
                NotifyPropertyChanged("IsSelected");
            }
        }

        private int _value;
        public int Value
        {
            get
            {
                return _value;
            }
            set
            {
                _value = value;
                NotifyPropertyChanged("Value");
            }
        }
        private string _name;
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
                NotifyPropertyChanged("Name");
            }
        }

        private void NotifyPropertyChanged(string p)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(p));
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }

    public class MyConverter : IValueConverter
    {

        #region IValueConverter Members



        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {

            return ((bool)value) ? Visibility.Visible : Visibility.Hidden;

        }



        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {

            throw new NotImplementedException();

        }



        #endregion

    }

Open in new window

0
 

Author Comment

by:Lukino2000
ID: 33704341
Sorry gauthampj,

your solution would work but it has hardcoded the width of elements. Can you provide please a solution that adapts itself at the width of listbox?
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 33704390
are you talking about the width of the text blocks ?
0
 
LVL 29

Accepted Solution

by:
Gautham Janardhan earned 500 total points
ID: 33704431
if so then replace the template with this
<DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition />
                            <ColumnDefinition />
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>
                        <TextBlock Grid.Column="0" Text=">" Visibility="{Binding Mode=TwoWay, Path=IsSelected,Converter={StaticResource temp}}"/>
                        <TextBlock Grid.Column="1" Text="{Binding Name}"/>
                        <TextBlock Grid.Column="2" HorizontalAlignment="Right" Text="{Binding Value}"/>
                    </Grid>
                </DataTemplate>

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now