2013-10-03 3 views
0

의도 한대로 도구 설명을 사용하고 싶습니다. 그러나 오류가 발생하면 오류 메시지가 표시되도록 변경 한 다음 오류가 수정되면 다시 변경합니다.WPF에서 도구 설명에 대한 연결된 속성을 만듭니다.

그래서 툴팁을 보유 할 첨부 속성을 만들었습니다. 첨부 된 속성에 툴팁을 할당 한 다음 스타일을 사용하여 툴팁 속성에 복사합니다. 오류가있는 경우 스타일은 도구 설명을 오류 메시지로 설정합니다. 툴팁 오류 메시지를 설정하는

그래서 트리거는 다음과 같습니다

오류가 수정 될 때

, 나는 다시 설정 매우 쉽게 보인다 (및 작동)

<Trigger Property="Validation.HasError" 
       Value="true"> 
<Setter Property="BorderBrush" 
       Value="{DynamicResource controls-errorBorderBrush}" /> 
<Setter Property="ToolTip" 
       Value="{Binding RelativeSource={x:Static RelativeSource.Self}, 
    Path=(Validation.Errors)[0].ErrorContent}" /> 
</Trigger> 

(이 '아무튼 t 일) :

<Trigger Property="Validation.HasError" 
Value="false"> 
<Setter Property="ToolTip" 
    Value="{Binding Path=(wpfMisc:myCtrl.tooltipValue)}" /> 
</Trigger> 

그리고 XAML 파일에

내가 가진 :

<TextBox Text="this is a textbox with a myMisc based tooltip" 
Name="txtTooltip2" 
wpfMisc:myCtrl.tooltipValue="Tooltip Test tooltip" /> 

물론 정보가 올바르게 저장되지 않는 것처럼 보이는 문제는 내 연결된 속성에서 발생할 가능성이 큽니다. 여기에 그 코드는 다음과 같습니다

public static string GettooltipValue(DependencyObject obj) 
{ 
string value = obj.GetValue(tooltipValueProperty).ToString() ; 
value = value.trimNull() ; // extension method to insure at least an empty string 
return value ; 
} 

public static void SettooltipValue(DependencyObject obj, string value) 
{ 
obj.SetValue(tooltipValueProperty, value.trimNull()); 
} 

public static readonly DependencyProperty tooltipValueProperty = 
DependencyProperty.RegisterAttached("tooltipValue", 
typeof(string), 
typeof(myCtrl), 
new UIPropertyMetadata(string.Empty)); 

그래서 내 생각 엔 내가 UIPropertyMetaData 다른 뭔가를 사용해야한다는 것입니다,하지만 난 사용하는 것이 무엇인지 확실하지. 아니면 내 접근 방식이 틀렸어?

모든 데이터 필드에 대해 특정 도구 설명이 필요합니다.

오류가 발생하는 동안 툴팁을 태그 속성으로 옮겨서이 작업을 수행했지만, 다른 코드가이 코드를 사용하려고 할 때 문제가 발생한다는 것을 알고 있기 때문에이를 그대로두고 싶지 않았습니다. 태그를 사용하는 것이 좋습니다.

... 디버깅 단지 부작용 및 myCtrl의 다른 종속성 속성은 잘 작동, 그래서 참조는 등의 XMLNS 알 -

또한, 나는 코드의 일부는 자세한 것을 알고 옳은.

추가 조사에서 출력 창에서 다음을 찾았습니다. System.Windows.Data 오류 : 17 : ''(type 'layoutSettingsViewModel')에서 'tooltipValue'값 (type 'String')을 가져올 수 없습니다. BindingExpression : Path = (0); DataItem = 'layoutSettingsViewModel'(HashCode = 46457861); 대상 요소가 'TextBox'(Name = ''); 대상 속성 'ToolTip'('Object'형식) InvalidCastException : 'System.InvalidCastException : 형식'client.Models.layoutSettings.layoutSettingsViewModel '형식의 개체를'System.Windows.DependencyObject '입력 할 수 없습니다.

layoutSettingsViewModel은 xaml보기입니다. 그래서 뷰 ​​자체가 어떻게 든 컨트롤 대신 가치를 얻고 있다고 생각합니다. 확실하지 않습니다. 여러분 중 하나가 정확히 무엇을 의미하고 있는지, 그리고 왜 ... 정확히 알고 있는지 추측합니다. 속도를 높이려고하는 것이 싫습니다. 새로운 언어 ...

어쨌든, 어떤 도움이나 제안을 부탁드립니다.

+0

당신은 때 검증 출력 창에있는 바인딩 오류를 얻고있다 HasError가 false로 설정 되었습니까? 어떻게 그리고 어디에서 wpfMisc : myCtrl.tooltipValue를 설정하고 있습니까? 또한 이러한 트리거를 적용하는 컨트롤을 공유 할 수 있습니까? – Nitin

+0

2 개의 것들부터 시작할 것입니다. 먼저 obj.GetValue wpf (C#)가 연결된 속성 코드를 최적화 할 수 있으므로 다른 모든 코드가 필요하지 않으며 일반적으로 기본 구현이 될 것으로 예상됩니다. (다만 확실한) 두 번째로 dp에 대한 콜백을 추가하고 실제로 값이 변경되면 언제, 무엇을 제외하고 나에게 무슨 일이 일어나는지 알려주세요. –

+0

nit, 나는 코드를 가지고 있었지만, 어떤 이유로 든 보이지 않았다. 지금이야. 그리고 나는 데이터 입력 컨트롤과 함께 사용하기를 원할지라도 간단한 텍스트 상자로 테스트 해왔다. – JustMeToo

답변

0

나는 비슷한 기능을 만들었지 만, Button 컨트롤을 만들었습니다. 이것에 대한 작업 코드를 제공해 드리며 사용하고자하는 컨트롤을 Button으로 바꾸면됩니다.

private static readonly DependencyPropertyKey originalToolTipPropertyKey = DependencyProperty.RegisterAttachedReadOnly("OriginalToolTip", typeof(string), typeof(ButtonProperties), new FrameworkPropertyMetadata(default(string))); 

/// <summary> 
/// Contains the original Button.ToolTip value to display when the Button.IsEnabled property value is set to true. 
/// </summary> 
public static readonly DependencyProperty OriginalToolTipProperty = originalToolTipPropertyKey.DependencyProperty; 

/// <summary> 
/// Gets the value of the OriginalToolTip property. 
/// </summary> 
/// <param name="dependencyObject">The DependencyObject to return the OriginalToolTip property value from.</param> 
/// <returns>The value of the OriginalToolTip property.</returns> 
public static string GetOriginalToolTip(DependencyObject dependencyObject) 
{ 
    return (string)dependencyObject.GetValue(OriginalToolTipProperty); 
} 

/// <summary> 
/// Provides Button controls with an additional tool tip property that only displays when the Button.IsEnabled property value is set to false. 
/// </summary> 
public static DependencyProperty DisabledToolTipProperty = DependencyProperty.RegisterAttached("DisabledToolTip", typeof(string), typeof(ButtonProperties), new UIPropertyMetadata(string.Empty, OnDisabledToolTipChanged)); 

/// <summary> 
/// Gets the value of the DisabledToolTip property. 
/// </summary> 
/// <param name="dependencyObject">The DependencyObject to return the DisabledToolTip property value from.</param> 
/// <returns>The value of the DisabledToolTip property.</returns> 
public static string GetDisabledToolTip(DependencyObject dependencyObject) 
{ 
    return (string)dependencyObject.GetValue(DisabledToolTipProperty); 
} 

/// <summary> 
/// Sets the value of the DisabledToolTip property. 
/// </summary> 
/// <param name="dependencyObject">The DependencyObject to set the DisabledToolTip property value of.</param> 
/// <param name="value">The value to be assigned to the DisabledToolTip property.</param> 
public static void SetDisabledToolTip(DependencyObject dependencyObject, string value) 
{ 
    dependencyObject.SetValue(DisabledToolTipProperty, value); 
} 

/// <summary> 
/// Adds ro removes event handlers to the Button control that updates the Button.ToolTip value to the DisabledToolTip property value when the Button.IsEnabled property value is set to false. 
/// </summary> 
/// <param name="dependencyObject">The Button object.</param> 
/// <param name="e">The DependencyPropertyChangedEventArgs object containing event specific information.</param> 
public static void OnDisabledToolTipChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) 
{ 
    Button button = dependencyObject as Button; 
    if (button != null && e.OldValue != e.NewValue) button.IsEnabledChanged += Button_IsEnabledChanged; 
    else if (e.OldValue != null && e.NewValue == null) button.IsEnabledChanged -= Button_IsEnabledChanged; 
} 

private static void Button_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e) 
{ 
    Button button = sender as Button; 
    if (GetOriginalToolTip(button) == null) button.SetValue(originalToolTipPropertyKey, button.ToolTip.ToString()); 
    button.ToolTip = (bool)e.NewValue ? GetOriginalToolTip(button) : GetDisabledToolTip(button); 
} 

다음과 같이 사용됩니다 : 관심있는 사람들을위한

<Button ToolTip="Normal ToolTip text to display" 
    Attached:ButtonProperties.DisabledToolTip="Text to automatically display when 
    Button is disabled"> 
+0

나는 156이라는 라인이 + = 대신 = = (아니면 내가 뭘 분명히 알지 못 하겠는가?)라고 가정한다. – JustMeToo

+0

OnDisabledToolTipChanged에서 두 번째 것이다. (라인 #이 일치하지 않기 때문에 코드를 다시 포맷했다.) 또한, 단추를 사용할 수 없을 때 도구 설명을 표시 할 수 없습니다. 일반 도구 설명 만 사용하는 경우에도 마찬가지입니다. 나는 다른 것을 놓치고 있니? – JustMeToo

+0

하하, 좋은 점은 '- =', 고마워! 정상적으로 작동해야하지만, 일반 ToolTip * 및 * DisabledToolTip 속성을 모두 설정해야합니다. 그것은 저에게 적합합니다. 'Button_IsEnabledChanged' 핸들러에 중단 점을 넣어 호출되는지 확인하십시오. 그렇지 않으면, 당신은'AttachedProperty' 클래스에 어떤 문제가있을 수 있습니다. – Sheridan

0

는, 여기에 기본적인 나는 원래 값을 '기억'에 장애인 ToolTip 메시지 하나 AttachedProperty 다른를 만들어야했습니다 논리 - Sheridan이 공유 한 코드를 기반으로합니다. 이것이 더 간결해질 수 있다는 것을 알고 있습니다. 그러나 이것은 새로운 WPF 개발자가 어떻게 작동하는지 쉽게 볼 수있게합니다. 여기

는 XAML 스타일입니다 - 툴팁 및 데이터를 지원하는 모든 제어에 사용 할 수 있습니다 : 여기

<Style TargetType="TextBox"> 
<Style.Triggers> 
    <Trigger Property="Validation.HasError" 
         Value="true"> 
     <!-- We have an error, set the ErrorToolTip attached property to 
     the error. When the error is no more, it is automatically set 
     back to the original value (blank) so no need for a 2nd trigger --> 
     <Setter Property="wpfMisc:myCtrl.ErrorToolTip" 
         Value="{Binding RelativeSource={x:Static RelativeSource.Self}, 
     Path=(Validation.Errors)[0].ErrorContent}" /> 

    </Trigger> 

</Style.Triggers> 
</Style> 

을 그리고 그 보유 (여기 myCtrl를 사용하여) 클래스에 추가 할 수있는 코드는 종속성 속성은/속성 :

물론
/// <summary> 
/// Holds the default Tooltip value. OnMyToolTipChanged used to set ToolTip 
/// </summary> 
public static DependencyProperty MyToolTipProperty = DependencyProperty.RegisterAttached("MyToolTip", typeof(string), typeof(myCtrl), new UIPropertyMetadata(string.Empty, OnMyToolTipChanged)); 

/// <summary> 
/// Gets the value of the MyToolTip property. 
/// </summary> 
/// <param name="dependencyObject">The DependencyObject to return the MyToolTip property value from.</param> 
/// <returns>The value of the MyToolTip property.</returns> 
public static string GetMyToolTip(DependencyObject dependencyObject) 
{ 
    return (string)dependencyObject.GetValue(MyToolTipProperty); 
} 

/// <summary> 
/// Sets the value of the MyToolTip property. 
/// </summary> 
/// <param name="dependencyObject">The DependencyObject to set the MyToolTip property value of</param> 
/// <param name="value">The value to be assigned to the MyToolTip property.</param> 
public static void SetMyToolTip(DependencyObject dependencyObject, string value) 
{ 
    dependencyObject.SetValue(MyToolTipProperty, value); 
} 

/// <summary> 
/// Initially blank, set by style when an error occures (or goes away). Uses OnErrorToolTipChanged to update ToolTip. 
/// </summary> 
public static DependencyProperty ErrorToolTipProperty = DependencyProperty.RegisterAttached("ErrorToolTip", typeof(string), typeof(myCtrl), new UIPropertyMetadata(string.Empty, OnErrorToolTipChanged)); 

/// <summary> 
/// Gets the value of the ErrorToolTip property. 
/// </summary> 
/// <param name="dependencyObject">The DependencyObject to return the ErrorToolTip property value from</param> 
/// <returns>The value of the ErrorToolTip property.</returns> 
public static string GetErrorToolTip(DependencyObject dependencyObject) 
{ 
    return (string)dependencyObject.GetValue(ErrorToolTipProperty); 
} 

/// <summary> 
/// Sets the value of the ErrorToolTip property. 
/// </summary> 
/// <param name="dependencyObject">The DependencyObject to set the ErrorToolTip property value of</param> 
/// <param name="value">The value to be assigned to the ErrorToolTip property.</param> 
public static void SetErrorToolTip(DependencyObject dependencyObject, string value) 
{ 
    dependencyObject.SetValue(ErrorToolTipProperty, value); 
} 

/// <summary> 
/// If an Error Tooltip is supplied, sets the ToolTip to that value, otherwise, resets it back to MyToolTipProperty 
/// </summary> 
/// <param name="dependencyObject">The control with the tooltip</param> 
/// <param name="e">The DependencyPropertyChangedEventArgs object containing event specific information.</param> 
public static void OnErrorToolTipChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) 
{ 
    if (dependencyObject is TextBox) 
    { 
     var txtControl = dependencyObject as TextBox; 
     if (e.NewValue == null || e.NewValue.ToString() == string.Empty) 
     { 
      // No tooltip, reset to the original value 
      txtControl.ToolTip = (string)dependencyObject.GetValue(MyToolTipProperty); 
     } 
     else 
     { 
      // Use the error tooltip 
      txtControl.ToolTip = e.NewValue; 
     } 
    } 
} 

/// <summary> 
/// This should only be called when the value is first assigned to the control. 
/// </summary> 
/// <param name="dependencyObject">The Control</param> 
/// <param name="e">The DependencyPropertyChangedEventArgs object containing event 
/// specific information.</param> 
public static void OnMyToolTipChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) 
{ 
    // What type of control - I may be able to use a generic parent that supports Tooltips, but until I have time to figure that out, using this to generate a valid control.ToolTip reference. 
    if (dependencyObject is TextBox) 
    { 
     var txtControl = dependencyObject as TextBox; 
     if (e.OldValue != e.NewValue) 
     { 
      txtControl.ToolTip = e.NewValue; 
     } 
    } 
    else if (dependencyObject is ComboBox) 
    { 
     // Add code here for ComboBox and other tooltip controls (if we can't use a parent/interface reference instead.) 
    } 
} 

,이 등 스타일에 훨씬 더 그러나 당신이이 문제를 해결하는 데 필요한 코드 만했다 그래서 나는 모든을 떠났다. 이 사람하는 데 도움이

희망 ...

(당신은 당신이 더 나은 그것을 할 것이, 더 많은 아이디어, 갈 수 있다고 생각하면 더 나은)

관련 문제