wpf进度条绑定到列表框值
如何根据列表框值或组合框值移动进度条值(我使用进度条模拟汽车时速圆形仪表)?我用一个长方形做针 我可以用一个滚动条(滚动条的值使针移动)来完成它,这就是我将显示的代码。我希望能够在列表框、组合框中设置不同的速度,而不是滚动条的值,当选中时,进度条/矩形将移动到该值 这能做到吗 我将只显示我认为您需要查看的代码。。 下面是一张我正在谈论的图片:wpf进度条绑定到列表框值,wpf,xaml,Wpf,Xaml,如何根据列表框值或组合框值移动进度条值(我使用进度条模拟汽车时速圆形仪表)?我用一个长方形做针 我可以用一个滚动条(滚动条的值使针移动)来完成它,这就是我将显示的代码。我希望能够在列表框、组合框中设置不同的速度,而不是滚动条的值,当选中时,进度条/矩形将移动到该值 这能做到吗 我将只显示我认为您需要查看的代码。。 下面是一张我正在谈论的图片: <Window.Resources> <ControlTemplate x:Key="templateSpeedometer"
<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>
我将使用(进度条的)Value属性更改TemplateSpeedMeter的设计,并使用ValueConverter将值转换为角度
<RotateTransform
Angle="{RelativeSource={RelativeSource TemplatedParent}, Path=Value,
Converter={StaticResource valueToAngleConverter}}" />
这样,您可以将仪表的值绑定到轨迹栏、滚动条、ListBox.SelectedValue或任何您喜欢的内容
编辑
我调整了代码。是的,可以这样做。如果在
列表框
(或组合框
)中显示了值列表,则可以通过属性绑定到选定的值
为了更好地控制总体设计,请查看以下博客:
同意-这是一个更好的设计,但没有直接回答问题,即如何绑定到列表框中的项。我发现您的代码有错误--错误MC3045:解析标记扩展时遇到类型为“System.Windows.TemplateBindingExtension”的未知属性“Path”。第254行位置46。@icelated,Sorry Path应该是property,代码用蓝色突出显示,告诉我如果不是在模板中,就不能设置TemplateBinding。。我不知道该怎么办?啊,像这样更改绑定:我试图通过SelectedItem Property绑定,但它不起作用,我可以看到我得到了一个转换器错误。System.Windows.Data错误:6:'ObjectSourceConverter'转换器无法转换值'System.Windows.Controls.ListBoxItem:10'(类型'ListBoxItem');如果可用,将使用回退值。BindingExpression:Path=SelectedItem;DataItem='ListBox'(Name='scroll2');目标元素是“ProgressBar”(名称=“”);目标属性为'Value'(类型'Double')NotSupportedException:'System.NotSupportedException:DoubleConverter无法从System.Windows.Controls.ListBoxItem.Ah转换。。。那么您正在手动将ListBoxItems添加到ListBox?在这种情况下,您需要将列表框绑定到要从中选择的项目列表。例如,MyListBox.ItemsSource=新列表(){10,20,30,40};这将导致SelectedItem成为double.Cool-是否要“勾选”我的回答作为答案?