如何使用 LiveChart Gauge 使 LabelFormatter 仅影响一个绑定值
How to make LabelFormatter only effect one bindling value by using LiveChart Gauge
我正在使用 LiveChart Gauge 制作仪表板程序。我想用 %
符号显示值,但它也改变了我的最大值。
我怎样才能只影响 1 个绑定值而不是所有绑定值?
XAML
<lvc:Gauge Grid.Row="2"
Grid.Column="0"
Margin="15"
Foreground="White"
GaugeActiveFill="Yellow"
From="0"
To="{Binding totalTargetQuantity}"
Value="{Binding ChartValue2}"
LabelFormatter="{Binding Formatter2}"/>
使用labelformatter之前
使用labelformatter后
我需要的结果
谁能帮我解决这个问题?
很遗憾,您不能这样做,因为 LiveCharts Gauge 不提供单独设置标签格式的选项。你可以做的是隐藏底部标签并提供你自己的标签,但你必须手动定位它,例如:
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
</Grid.RowDefinitions>
<lvc:Gauge
x:Name="myGauge"
Margin="15,35"
LabelFormatter="{Binding Formatter2}"
LabelsVisibility="Collapsed"
StrokeThickness="2"
Value="9"
From="0"
To="10"
/>
<Label Content="{Binding ElementName=myGauge, Path=From}" FontSize="20" Margin="100,0" VerticalAlignment="Bottom"/>
<Label Content="{Binding ElementName=myGauge, Path=To}" FontSize="20" Margin="100,0" VerticalAlignment="Bottom" HorizontalAlignment="Right"/>
</Grid>
编辑:
或者在ControlTemplate中添加标签:
<Window.Resources>
<Style x:Key="GaugeStyle1" TargetType="{x:Type lvc:Gauge}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type lvc:Gauge}">
<Border
Padding="{TemplateBinding Padding}"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
SnapsToDevicePixels="True">
<Grid>
<ContentPresenter
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Content="{TemplateBinding Content}"
ContentStringFormat="{TemplateBinding ContentStringFormat}"
ContentTemplate="{TemplateBinding ContentTemplate}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="3*" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Label
Grid.Row="2"
HorizontalAlignment="Center"
VerticalAlignment="Bottom"
Content="{Binding ElementName=myGauge, Path=From}"
FontSize="{TemplateBinding FontSize}" />
<Label
Grid.Row="2"
Grid.Column="2"
HorizontalAlignment="Center"
VerticalAlignment="Bottom"
Content="{Binding ElementName=myGauge, Path=To}"
FontSize="{TemplateBinding FontSize}" />
</Grid>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<lvc:Gauge
x:Name="myGauge"
Margin="15"
FontSize="30"
LabelFormatter="{Binding Formatter2}"
LabelsVisibility="Collapsed"
StrokeThickness="2"
Style="{DynamicResource GaugeStyle1}"
Value="9"
From="0"
To="10" />
</Grid>
首先,没有办法“开箱即用”。所以每个解决方案都会很“特别”,甚至看起来像个拐杖。
不确定我应该按什么顺序排列它们。
隐藏原始仪表标签并创建特殊的UserControl/Adorner以用您喜欢的文本覆盖它们
下载LiveCharts源代码,在“WpfView”解决方案中找到Gauge.cs。找私人
更新()方法。
您正在寻找下一行:
LeftLabel.Text = (LabelFormatter ?? defFormatter)(From);
RightLabel.Text = (LabelFormatter ?? defFormatter)(To);
克隆仪表,或为每个标签引入单独的 LabelFormatter。
想不出别的了。要么你“伪造”原始标签,要么你改变来源,因为没有办法覆盖这种行为。
与 Alexandr 相同的答案,但有修改:
下载并使用 dnspy 打开 LiveCharts.Wpf.dll 并找到行:
this.MeasureTextBlock.Text = (this.LabelFormatter ?? func)(this.Value);
单击“编辑方法”并将该行替换为:
this.MeasureTextBlock.Text = "% " + (this.LabelFormatter ?? func)(this.Value);
现在点击编译。
文件 -> 保存模块 -> LiveCharts.Wpf.dll
现在这个 dll 将显示仪表如下:
我正在使用 LiveChart Gauge 制作仪表板程序。我想用 %
符号显示值,但它也改变了我的最大值。
我怎样才能只影响 1 个绑定值而不是所有绑定值?
XAML
<lvc:Gauge Grid.Row="2"
Grid.Column="0"
Margin="15"
Foreground="White"
GaugeActiveFill="Yellow"
From="0"
To="{Binding totalTargetQuantity}"
Value="{Binding ChartValue2}"
LabelFormatter="{Binding Formatter2}"/>
使用labelformatter之前
使用labelformatter后
我需要的结果
谁能帮我解决这个问题?
很遗憾,您不能这样做,因为 LiveCharts Gauge 不提供单独设置标签格式的选项。你可以做的是隐藏底部标签并提供你自己的标签,但你必须手动定位它,例如:
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
</Grid.RowDefinitions>
<lvc:Gauge
x:Name="myGauge"
Margin="15,35"
LabelFormatter="{Binding Formatter2}"
LabelsVisibility="Collapsed"
StrokeThickness="2"
Value="9"
From="0"
To="10"
/>
<Label Content="{Binding ElementName=myGauge, Path=From}" FontSize="20" Margin="100,0" VerticalAlignment="Bottom"/>
<Label Content="{Binding ElementName=myGauge, Path=To}" FontSize="20" Margin="100,0" VerticalAlignment="Bottom" HorizontalAlignment="Right"/>
</Grid>
编辑: 或者在ControlTemplate中添加标签:
<Window.Resources>
<Style x:Key="GaugeStyle1" TargetType="{x:Type lvc:Gauge}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type lvc:Gauge}">
<Border
Padding="{TemplateBinding Padding}"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
SnapsToDevicePixels="True">
<Grid>
<ContentPresenter
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Content="{TemplateBinding Content}"
ContentStringFormat="{TemplateBinding ContentStringFormat}"
ContentTemplate="{TemplateBinding ContentTemplate}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="3*" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Label
Grid.Row="2"
HorizontalAlignment="Center"
VerticalAlignment="Bottom"
Content="{Binding ElementName=myGauge, Path=From}"
FontSize="{TemplateBinding FontSize}" />
<Label
Grid.Row="2"
Grid.Column="2"
HorizontalAlignment="Center"
VerticalAlignment="Bottom"
Content="{Binding ElementName=myGauge, Path=To}"
FontSize="{TemplateBinding FontSize}" />
</Grid>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<lvc:Gauge
x:Name="myGauge"
Margin="15"
FontSize="30"
LabelFormatter="{Binding Formatter2}"
LabelsVisibility="Collapsed"
StrokeThickness="2"
Style="{DynamicResource GaugeStyle1}"
Value="9"
From="0"
To="10" />
</Grid>
首先,没有办法“开箱即用”。所以每个解决方案都会很“特别”,甚至看起来像个拐杖。
不确定我应该按什么顺序排列它们。
隐藏原始仪表标签并创建特殊的UserControl/Adorner以用您喜欢的文本覆盖它们
下载LiveCharts源代码,在“WpfView”解决方案中找到Gauge.cs。找私人 更新()方法。 您正在寻找下一行:
LeftLabel.Text = (LabelFormatter ?? defFormatter)(From); RightLabel.Text = (LabelFormatter ?? defFormatter)(To);
克隆仪表,或为每个标签引入单独的 LabelFormatter。
想不出别的了。要么你“伪造”原始标签,要么你改变来源,因为没有办法覆盖这种行为。
与 Alexandr 相同的答案,但有修改:
下载并使用 dnspy 打开 LiveCharts.Wpf.dll 并找到行:
this.MeasureTextBlock.Text = (this.LabelFormatter ?? func)(this.Value);
单击“编辑方法”并将该行替换为:
this.MeasureTextBlock.Text = "% " + (this.LabelFormatter ?? func)(this.Value);
现在点击编译。
文件 -> 保存模块 -> LiveCharts.Wpf.dll
现在这个 dll 将显示仪表如下: