저는 VB.net에서 별도의 프로젝트로 작성된 모든 계산 기능을 가진 엔지니어링 프로그램을 작성 중이며 현재 WPF UI를 사용하고 있습니다.WPF의 동적 조건부 서식
단위 사이에서 문자열 형식을 변경하면 문제가 발생했습니다. 예 : Imperial Units에서 4,966 lbf의 값을 가지며 변환 된 값은 22.1 kN입니다. 서로 다른 차수의 형식이므로 2 사이에 다른 형식이 있어야한다는 것을 알 수 있습니다.
현재 프로그램에 설정되어있는 것은 조건부 채색 (일반 숫자는 검은 색, 오류는 빨간색, 경고는 노란색)이며 이는 리소스 사전의 스타일을 통해 설정됩니다.
<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>
프로그램에서 스타일은 변환기와 멀티 바인딩을 사용하여 선택됩니다. 다음 MultiValueConverter을
<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>
을 그리고 : ValueShow.TensionStatusShow
는 열거 값이 VB 계산 코드에서오고있다
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;
}
}
내가 무엇을 시도했다 :는 그래서 여기에 문제는 내가 원하는 것을 ValueShow.TensionStatusShow
과 달리 VB 계산 방법에서 문자열 서식 지정 "규칙"을 유지하십시오. 현재 우리는 유닛 레이블에 대한 문자열을 보유하는 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>
그럼 난 multibinding의 세 번째 매개 변수로 lbkNFormatting
을 전달하고 TryFindResource
통화에 추가 할 것입니다. 이 분명히 작동하지 않았다, 그것은 성공적으로 리소스를로드 할 수 있지만 문자열 형식을 무시합니다. 로드 된 Metric 리소스에 배경색을 추가하여 테스트했지만 문자열 형식이 무시되었습니다.
StringFormat을 정적 대신 DynamicResource로 변경해 보았습니까? – landoncz
방금 시도했는데 다음 오류가 발생합니다. 'DynamicResourceExtension'을 '바인딩'유형의 'StringFormat'속성에서 설정할 수 없습니다. 'DynamicResourceExtension'은 DependencyObject의 DependencyProperty에서만 설정할 수 있습니다. – user3565590