将Listbox SelectedItems绑定到WPF中的Slider

时间:2021-05-20 16:05:32

I am relatively new to WPF, but can you provide an example of how to bind a slider value to a selected item in a listbox. Ideally, as the user moves the slider, the onscreen item changes dynamically as the slider value changes. From reading, I understand that you need to use an attached property or value converter since we are binding different types of values. If so, can you provide a simple example of the most efficient method.

我是WPF的新手,但是你能举例说明如何将滑块值绑定到列表框中的选定项目。理想情况下,当用户移动滑块时,屏幕上的项目会随着滑块值的变化而动态变化。通过阅读,我了解您需要使用附加属性或值转换器,因为我们绑定了不同类型的值。如果是这样,你能提供一个最有效的方法的简单例子。

Thank you.

1 个解决方案

#1


0  

Not sure if this really is the question, but in case you mean to move the selected item through the ListBox items you could directly bind the SelectedIndex of the ListBox to the Slider Value:

不确定这是否真的是问题,但是如果你想通过ListBox项移动所选项,你可以直接将ListBox的SelectedIndex绑定到Slider值:

<StackPanel>
    <ListBox SelectedIndex="{Binding Value, ElementName=slider}">
        <ListBoxItem>1</ListBoxItem>
        <ListBoxItem>2</ListBoxItem>
        ...
        <ListBoxItem>10</ListBoxItem>
    </ListBox>
    <Slider Name="slider" Minimum="0" Maximum="9"/>
</StackPanel>

Of course you would have to adjust the Slider's Maximum value to the actual number of items minus one.

当然,您必须将滑块的最大值调整为实际的项目数减1。

#1


0  

Not sure if this really is the question, but in case you mean to move the selected item through the ListBox items you could directly bind the SelectedIndex of the ListBox to the Slider Value:

不确定这是否真的是问题,但是如果你想通过ListBox项移动所选项,你可以直接将ListBox的SelectedIndex绑定到Slider值:

<StackPanel>
    <ListBox SelectedIndex="{Binding Value, ElementName=slider}">
        <ListBoxItem>1</ListBoxItem>
        <ListBoxItem>2</ListBoxItem>
        ...
        <ListBoxItem>10</ListBoxItem>
    </ListBox>
    <Slider Name="slider" Minimum="0" Maximum="9"/>
</StackPanel>

Of course you would have to adjust the Slider's Maximum value to the actual number of items minus one.

当然,您必须将滑块的最大值调整为实际的项目数减1。