【问题标题】:wpf progress bar bind to a listbox valuewpf进度条绑定到列表框值
【发布时间】:2018-08-26 04:33:26
【问题描述】:

如何根据列表框值或组合框值移动进度条值(我正在使用进度条模拟汽车 mph 圆形仪表)?我用一个矩形做针。

我可以使用滚动条(滚动条的值使指针移动)来实现,这是我将展示的代码。 Instead of the value of the scroll bar i want to be able to have various speeds set in a listbox, combobox and when selected the progress bar / rectangle will move to that value.

这个可以吗?

我只会显示我认为您需要查看的代码.. 这是我正在谈论的图片:

<Window.Resources>
<ControlTemplate x:Key="templateSpeedometer"
                     TargetType="ProgressBar">
    <ControlTemplate.Resources>
        <Style TargetType="Line">
        </Style>
    </ControlTemplate.Resources>


        <Canvas Width="0" Height="0" 
                    RenderTransform="1 0 0 1 0 50" Background="#FFF50D0D">

            <Rectangle Name="PART_Track" Width="180" />
            <Rectangle Fill="Black" Name="PART_Indicator" />



            <Polygon Points="5 2 5 -5 -75 0"
                         Stroke="Black" Fill="Gold">
                <Polygon.RenderTransform>
                    <RotateTransform 
                            Angle="{Binding ElementName=PART_Indicator, 
                                            Path=ActualWidth}" />
                </Polygon.RenderTransform>
            </Polygon>
        </Canvas>
    </Border>
</ControlTemplate>
</Window.Resources>

<Grid x:Name="LayoutRoot">

    <StackPanel>
        <Grid Height="216" Name="grid1" Width="612">
            <ScrollBar Name="scroll" Orientation="Horizontal" Minimum="0" Maximum="100" SmallChange="1" LargeChange="10" Margin="8,235,4,-36" />
            <Border Background="#FF5EB6D8"  CornerRadius="25" Height="247" VerticalAlignment="Top" Margin="13,5,27,0">

            <ProgressBar Background="#FFD6E1E5" Margin="4,8,0,112" Maximum="100" Minimum="0" Template="{StaticResource templateSpeedometer}" Value="{Binding ElementName=scroll, Path=Value}" BorderBrush="#FF5EB6D8" OpacityMask="White" HorizontalAlignment="Left" Width="281" BorderThickness="5,1,1,1" Orientation="Vertical"/>
            </Border>

【问题讨论】:

    标签: wpf xaml


    【解决方案1】:

    是的,这可以做到。如果您有一个在ListBox(或ComboBox)中显示的值列表,您可以通过SelectedItem 属性绑定到选定的值。

    要获得更好的控件整体设计,请查看此博文:

    http://www.scottlogic.co.uk/blog/colin/2011/02/a-circular-progressbar-style-using-an-attached-viewmodel/

    【讨论】:

    • 我尝试通过 SelectedItem 属性进行绑定,但它什么也没做,我可以看到我收到转换器错误。 System.Windows.Data 错误:6:“ObjectSourceConverter”转换器无法转换值“System.Windows.Controls.ListBoxItem:10”(类型“ListBoxItem”);如果可用,将使用后备值。绑定表达式:路径=选定项; DataItem='ListBox' (Name='scroll2');目标元素是'ProgressBar'(名称='');目标属性是'Value'(类型'Double') NotSupportedException:'System.NotSupportedException: DoubleConverter cannot convert from System.Windows.Controls.ListBoxItem.
    • 啊...所以您手动将 ListBoxItems 添加到您的 ListBox 中?在这种情况下,您需要将 ListBox 绑定到您希望从中选择的项目列表。例如。 MyListBox.ItemsSource = new List() { 10, 20, 30, 40 };这将导致 SelectedItem 成为双精度。
    • 酷 - 你想“勾选”我的回答作为答案吗?
    【解决方案2】:

    我会通过使用 Value 属性(进度条的)来更改模板Speedometer 的设计,并使用 ValueConverter 将 Value 转换为角度。

    <RotateTransform  
        Angle="{RelativeSource={RelativeSource TemplatedParent}, Path=Value, 
                Converter={StaticResource valueToAngleConverter}}" />                                    
    

    这样你可以将仪表的值绑定到 TrackBar、ScrollBar、ListBox.SelectedValue 或任何你喜欢的东西。

    编辑

    我调整了代码see here why I switched from TemplateBinding to Binding with RelativeSource

    【讨论】:

    • 同意 - 这是一个更好的设计,但没有直接回答问题,即如何绑定到列表框中的项目。
    • 我的代码出现错误 --error MC3045: Unknown property 'Path' for type 'System.Windows.TemplateBindingExtension' 在解析标记扩展时遇到。第 254 行第 46 行。
    • @icelated,抱歉路径应该是属性
    • 用你的代码将它突出显示为蓝色并告诉我如果不在模板中你不能设置 TemplateBinding。我不知道该怎么办?
    • 啊,把绑定改成这样:
    猜你喜欢
    • 2011-08-28
    • 1970-01-01
    • 1970-01-01
    • 2011-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-30
    相关资源
    最近更新 更多