【发布时间】:2015-01-09 14:57:58
【问题描述】:
我正在开发一个工程程序,该程序的所有计算都用 VB.net 编写在一个单独的项目中,我们现在将它放在 WPF UI 后面。
我遇到了在单位之间更改字符串格式的问题。例如:在英制单位中,您的值为 4,966 lbf,转换为 22.1 kN。您可以看到,两者之间必须有不同的格式,因为它们是不同的数量级。
程序中当前设置的是条件着色(黑色表示正常数字,红色表示错误,黄色表示警告)这些是通过资源字典中的样式设置的。
<Style x:Key="GlobalUserEditedTextBox" BasedOn="{StaticResource {x:Type TextBox}}" TargetType="TextBox">
<Setter Property="Foreground" Value="{DynamicResource EditableTextColor}"/>
<Setter Property="FontWeight" Value="Bold"/>
</Style>
<Style x:Key="GlobalErrorTextBox" BasedOn="{StaticResource {x:Type TextBox}}" TargetType="TextBox">
<Setter Property="Foreground" Value="{DynamicResource ErrorTextColor}"/>
<Setter Property="FontWeight" Value="Normal"/>
</Style>
在程序中,使用 Converter 和 MultiBinding 选择样式。 ValueShow.TensionStatusShow是VB计算代码出来的枚举值:
<TextBlock HorizontalAlignment="Stretch" TextAlignment="Center" Text="{Binding Path=ValueShow.TensionShow}">
<TextBlock.Style>
<MultiBinding Converter="{StaticResource styleConverter}">
<MultiBinding.Bindings>
<Binding RelativeSource="{RelativeSource Self}"/>
<Binding Path="ValueShow.TensionStatusShow"/>
</MultiBinding.Bindings>
</MultiBinding>
</TextBlock.Style>
</TextBlock>
然后是 MultiValueConverter:
public class StyleConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
FrameworkElement targetElement = values[0] as FrameworkElement;
Style _newStyle;
try
{
if (values[1] == null || values[1] == DependencyProperty.UnsetValue)
return null;
if ((String)values[1] == StatusColor.ErrorValue.ToString())
{
if (values[0].GetType() == typeof(TextBox))
_newStyle = (Style)targetElement.TryFindResource("GlobalErrorTextBox");
else if (values[0].GetType() == typeof(TextBlock))
_newStyle = (Style)targetElement.TryFindResource("GlobalErrorTextBlock");
else
_newStyle = null;
}
else if
{
if (values[0].GetType() == typeof(TextBox))
_newStyle = (Style)targetElement.TryFindResource("GlobalWarningTextBox");
else if (values[0].GetType() == typeof(TextBlock))
_newStyle = (Style)targetElement.TryFindResource("GlobalWarningTextBlock");
else
_newStyle = null;
}
return _newStyle;
}
catch (Exception)
{
if (values[0].GetType() == typeof(TextBox))
return (Style)targetElement.TryFindResource("GlobalUnEditableTextBox");
else if (values[0].GetType() == typeof(TextBlock))
return (Style)targetElement.TryFindResource("GlobalUnEditableTextBlock");
else
return null;
}
}
我尝试过的:
所以这里的问题是我想将字符串格式化“规则”排除在VB计算方法之外,这与ValueShow.TensionStatusShow不同。目前我们有 2 个资源字典(英制和公制)保存单位标签的字符串。我尝试在那里设置不同的字符串格式,以便在程序更改单位时更新。
帝国资源:
<s:String x:Key="UnitsStringFormatlbfkN">F0</s:String>
<Style TargetType="TextBox" x:Key="GlobalErrorTextBoxlbkNFormatting" BasedOn="{StaticResource GlobalErrorTextBox}">
<Setter Property="Text" Value="{Binding Path=., Mode=TwoWay, StringFormat={StaticResource UnitsStringFormatlbfkN}}" />
</Style>
指标资源
<s:String x:Key="UnitsStringFormatlbfkN">F1</s:String>
<Style TargetType="TextBox" x:Key="GlobalErrorTextBoxlbkNFormatting" BasedOn="{StaticResource GlobalErrorTextBox}">
<Setter Property="Text" Value="{Binding Path=., Mode=TwoWay, StringFormat={StaticResource UnitsStringFormatlbfkN}}" />
</Style>
然后我将lbkNFormatting 作为多重绑定中的第三个参数传递,并将其附加到TryFindResourcecall。这显然不起作用,它会成功加载资源,但它忽略了字符串格式。我通过向加载正常的 Metric 资源添加背景颜色进行了测试,但再次忽略了字符串格式。
我还尝试通过以编程方式添加字符串格式来修改 MultiValueConverter 中的样式,但遇到了我似乎无法击败的 IsSealed 属性
【问题讨论】:
-
您是否尝试将 StringFormat 更改为 DynamicResource 而不是 static?
-
我刚刚尝试过,我收到以下错误:无法在“Binding”类型的“StringFormat”属性上设置“DynamicResourceExtension”。只能在 DependencyObject 的 DependencyProperty 上设置“DynamicResourceExtension”。