【问题标题】:What is the error in this xaml code此 xaml 代码中的错误是什么
【发布时间】:2015-08-02 10:58:40
【问题描述】:

我希望路径由 prograssbar 填充,但这里发生了其他事情

<Window x:Class="WpfApplication6.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="ProgressBar" Height="300" Width="400">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Slider Grid.Row="0" Margin="5" Minimum="0" Maximum="100" Name="slider"/>
        <ProgressBar Grid.Row="1" Margin="5" Minimum="0" Maximum="100" Orientation="Horizontal"
                   Value="{Binding ElementName=slider, Path=Value}" Foreground="#FFD81818" Background="{x:Null}">
            <ProgressBar.Template>
                <ControlTemplate>
                    <Grid Background="{TemplateBinding Background}">
                       <Path Name="PART_Track" Height="26" Canvas.Left="37" Opacity="0.702" Canvas.Top="30" 
                                Stroke="Black" 
                                StrokeThickness="5" 
                                    Data="F1M307.5,21.75C303.765,21.75 300.51,19.701 298.793,16.667 298.793,16.667 242.207,16.667 242.207,16.667 240.49,19.701 237.235,21.75 233.5,21.75 229.765,21.75 226.51,19.701 224.793,16.667 224.793,16.667 168.207,16.667 168.207,16.667 166.49,19.701 163.235,21.75 159.5,21.75 155.765,21.75 152.51,19.701 150.793,16.667 150.793,16.667 94.207,16.667 94.207,16.667 92.49,19.701 89.235,21.75 85.5,21.75 81.765,21.75 78.51,19.701 76.793,16.667 76.793,16.667 20.207,16.667 20.207,16.667 18.49,19.701 15.235,21.75 11.5,21.75 5.977,21.75 1.5,17.273 1.5,11.75 1.5,6.227 5.977,1.75 11.5,1.75 15.167,1.75 18.37,3.724 20.111,6.667 20.111,6.667 76.889,6.667 76.889,6.667 78.63,3.724 81.833,1.75 85.5,1.75 89.167,1.75 92.37,3.724 94.111,6.667 94.111,6.667 150.889,6.667 150.889,6.667 152.63,3.724 155.834,1.75 159.5,1.75 163.167,1.75 166.37,3.724 168.111,6.667 168.111,6.667 224.889,6.667 224.889,6.667 226.63,3.724 229.834,1.75 233.5,1.75 237.167,1.75 240.37,3.724 242.111,6.667 242.111,6.667 298.889,6.667 298.889,6.667 300.63,3.724 303.833,1.75 307.5,1.75 313.023,1.75 317.5,6.227 317.5,11.75 317.5,17.273 313.023,21.75 307.5,21.75z"> 
                        </Path>
                        <Path Name="PART_Indicator" Height="26" Canvas.Left="37" Opacity="0.702" Canvas.Top="30" 
                                Stroke="Black" 
                                StrokeThickness="5" Fill="{TemplateBinding Foreground}"
                                    Data="F1M307.5,21.75C303.765,21.75 300.51,19.701 298.793,16.667 298.793,16.667 242.207,16.667 242.207,16.667 240.49,19.701 237.235,21.75 233.5,21.75 229.765,21.75 226.51,19.701 224.793,16.667 224.793,16.667 168.207,16.667 168.207,16.667 166.49,19.701 163.235,21.75 159.5,21.75 155.765,21.75 152.51,19.701 150.793,16.667 150.793,16.667 94.207,16.667 94.207,16.667 92.49,19.701 89.235,21.75 85.5,21.75 81.765,21.75 78.51,19.701 76.793,16.667 76.793,16.667 20.207,16.667 20.207,16.667 18.49,19.701 15.235,21.75 11.5,21.75 5.977,21.75 1.5,17.273 1.5,11.75 1.5,6.227 5.977,1.75 11.5,1.75 15.167,1.75 18.37,3.724 20.111,6.667 20.111,6.667 76.889,6.667 76.889,6.667 78.63,3.724 81.833,1.75 85.5,1.75 89.167,1.75 92.37,3.724 94.111,6.667 94.111,6.667 150.889,6.667 150.889,6.667 152.63,3.724 155.834,1.75 159.5,1.75 163.167,1.75 166.37,3.724 168.111,6.667 168.111,6.667 224.889,6.667 224.889,6.667 226.63,3.724 229.834,1.75 233.5,1.75 237.167,1.75 240.37,3.724 242.111,6.667 242.111,6.667 298.889,6.667 298.889,6.667 300.63,3.724 303.833,1.75 307.5,1.75 313.023,1.75 317.5,6.227 317.5,11.75 317.5,17.273 313.023,21.75 307.5,21.75z">
                        </Path>
                    </Grid>
                </ControlTemplate>
            </ProgressBar.Template>
        </ProgressBar>
        <TextBlock Grid.Row="2" Margin="5" Text="{Binding ElementName=slider, Path=Value}"/>
    </Grid> 
</Window>

生成的图片是这样的-

但我想要简单的这个-

根据进度值。

【问题讨论】:

  • PART_Indicator 似乎有一个隐式偏移。您可以通过翻译调整起点并将数量与进度绑定(可能与某种因素有关)。
  • 你能告诉我在上面的代码中要改变什么吗!
  • 恐怕我做不到。如果真的使用了Canvas.Left(您可以简单地检查一下),您可以将此值绑定到滑块值并使用特殊(自定义)值转换器进行数学运算。
  • 我是 .Net 的新手,所以我几乎不明白你在说什么,如果你能完全告诉我要在代码中更改/添加什么,这对我很有帮助。

标签: c# wpf xaml progress-bar


【解决方案1】:

只需将控件模板中的Grid更改为Canvas即可:

<ControlTemplate>
    <Canvas Background="{TemplateBinding Background}">
        <Path Name="PART_Track" Height="26" Canvas.Left="37" Opacity="0.702" Canvas.Top="30" 
                Stroke="Black" 
                StrokeThickness="5" 
                    Data="...">
        </Path>
        <Path Name="PART_Indicator" Height="26" Canvas.Left="37" Opacity="0.702" Canvas.Top="30" 
                Stroke="Black" 
                StrokeThickness="5" Fill="{TemplateBinding Foreground}"
                    Data="...">
        </Path>
    </Canvas>
</ControlTemplate>

【讨论】:

  • 我没有工作,只是把进度条放在上面。见postimg.org/image/fcmmjlf05
  • @RupamBhaduri 为我工作,查看结果图片;您似乎从PART_Indicator 中删除了Canvas.Top/Left 属性。那就是给你抵消。 PART_TrackPART_Indicator 必须相同
猜你喜欢
  • 2012-02-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-23
  • 1970-01-01
  • 2012-08-14
相关资源
最近更新 更多