开发者

WPF c# Listbox external scrollbuttons

Hi have an listbox and i want external buttons to scroll the listbox. How can i achive this. thanks fo开发者_StackOverflowr help and examples.


Add handlers for button clicks:

  private void buttonUp_Click(object sender, RoutedEventArgs e) {
   if (listBox1.SelectedIndex > 0) 
     listBox1.SelectedIndex--;
   listBox1.ScrollIntoView(listBox1.SelectedItem);

  }

  private void buttonDown_Click(object sender, RoutedEventArgs e) {
   if (listBox1.SelectedIndex < listBox1.Items.Count - 1) 
     listBox1.SelectedIndex++;
   listBox1.ScrollIntoView(listBox1.SelectedItem);
  }


XAML

<Window x:Class="WpfApplication36.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="479" Width="385">
<Grid Margin="10">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="*"></RowDefinition>
        <RowDefinition Height="Auto"></RowDefinition>
    </Grid.RowDefinitions>

    <ListBox Name="lstProducts"
             DisplayMemberPath="ModelName"
             IsSynchronizedWithCurrentItem="True"
             ScrollViewer.VerticalScrollBarVisibility="Hidden">

    </ListBox>

    <Border Grid.Row="1"
            Padding="5"
            Margin="0,5,0,5"
            Background="LightSteelBlue">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"></ColumnDefinition>
                <ColumnDefinition></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="*"></RowDefinition>
            </Grid.RowDefinitions>

            <TextBlock Margin="7">Model Number:</TextBlock>
            <TextBox Margin="5"
                     Grid.Column="1"
                     Text="{Binding Path=ModelNumber}"></TextBox>
            <TextBlock Margin="7"
                       Grid.Row="1">Model Name:</TextBlock>
            <TextBox Margin="5"
                     Grid.Row="1"
                     Grid.Column="1"
                     Text="{Binding Path=ModelName}"></TextBox>
            <TextBlock Margin="7"
                       Grid.Row="2">Unit Cost:</TextBlock>
            <TextBox Margin="5"
                     Grid.Row="2"
                     Grid.Column="1"
                     Text="{Binding Path=UnitCost}"></TextBox>
            <TextBlock Margin="7,7,7,0"
                       Grid.Row="3">Description:</TextBlock>
            <TextBox Margin="7"
                     Grid.Row="4"
                     Grid.Column="0"
                     Grid.ColumnSpan="2"
                     TextWrapping="Wrap"
                     VerticalScrollBarVisibility="Visible"
                     Text="{Binding Path=Description}"></TextBox>
        </Grid>
    </Border>

    <Grid Grid.Row="2">
        <StackPanel Orientation="Horizontal">
            <Button Name="cmdPrev"
                    Click="cmdPrev_Click">&lt;</Button>
            <TextBlock Margin="5,0,5,0"
                       Name="lblPosition"
                       VerticalAlignment="Center"></TextBlock>
            <Button Name="cmdNext"
                    Click="cmdNext_Click">&gt;</Button>
        </StackPanel>
    </Grid>
</Grid>

Code

namespace WpfApplication36{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
      private ICollection<Product> products;
    private ListCollectionView view;
    private void cmdNext_Click(object sender, RoutedEventArgs e)
    {    
        view.MoveCurrentToNext();          
    }
    private void cmdPrev_Click(object sender, RoutedEventArgs e)
    {
        view.MoveCurrentToPrevious();
    }

    private void lstProducts_SelectionChanged(object sender, RoutedEventArgs e)
    {
       // view.MoveCurrentTo(lstProducts.SelectedItem);
    }

    private void view_CurrentChanged(object sender, EventArgs e)
    {
        lblPosition.Text = "Record " + (view.CurrentPosition + 1).ToString() +
            " of " + view.Count.ToString();
        cmdPrev.IsEnabled = view.CurrentPosition > 0;
        cmdNext.IsEnabled = view.CurrentPosition < view.Count - 1; 
    }
    public Window1()
    {
         InitializeComponent();

        products = AddProduct() ;

        this.DataContext = products;
        view = (ListCollectionView)CollectionViewSource.GetDefaultView(this.DataContext);
        view.CurrentChanged += new EventHandler(view_CurrentChanged);

        lstProducts.ItemsSource = products;            

    }

    private Collection<Product> AddProduct()
    {

        Collection<Product> test = new Collection<Product>();
        Product prod=null;

        prod=new Product();
        prod.ModelName ="BMW";
            prod.ModelNumber ="Q234";
                prod.Description="BMWWWWWWWWWWWW";
                    prod.UnitCost="$3333333";
                    test.Add(prod);

                    prod = new Product();
                    prod.ModelName = "BMW11";
                    prod.ModelNumber = "Q234111";
                    prod.Description = "BMWWbbb";
                    prod.UnitCost = "$3333333";
                    test.Add(prod);

                    prod = new Product();
                    prod.ModelName = "BM3W";
                    prod.ModelNumber = "Q233334";
                    prod.Description = "BMWb33bbb";
                    prod.UnitCost = "$3333333";
                    test.Add(prod);
        return test;
    }



}

public class Product
{
    private string modelNumber;
    public string ModelNumber
    {
        get {return modelNumber;  }
        set{ modelNumber=value; }
    }
     private string modelName;
    public string ModelName
    {
        get {return modelName;  }
        set{ modelName=value ;}
    }
    private string unitCost;
    public string UnitCost
    {
        get { return unitCost; }
        set { unitCost = value; }
    }
    private string description;
    public string Description
    {
        get { return description; }
        set { description = value; }
    }


}

}


Heres my Code guys! Thanks for the help

        private void btnArrowUp_Click(object sender, RoutedEventArgs e) {
        if(lbZones.SelectedIndex > 0) {
            lbZones.SelectedIndex--;
            lbZones.ScrollIntoView(lbZones.SelectedIndex);
        }
    }

    private void btnArrowDown_Click(object sender, RoutedEventArgs e) {
        if(lbZones.SelectedIndex < lbZones.Items.Count - 1) {
            lbZones.SelectedIndex++;
            lbZones.ScrollIntoView(lbZones.SelectedIndex);
        }
    }

And heres the wpf

<Button Template="{StaticResource EmptyButton}" Name="btnArrowUp" Click="btnArrowUp_Click">
                                                <Canvas x:Name="ArrowUp" HorizontalAlignment="Left" VerticalAlignment="Top" Width="101" Height="72" ClipToBounds="True">
                                                    <Image x:Name="up" Width="100" Height="50" Source="pil 3_Images\Image.png" Canvas.Left="0" Canvas.Top="10.543"/>
                                                </Canvas>
                                            </Button>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜