2011-05-07 13 views
19

मुझे समस्या है और मुझे नहीं पता कि इस सरल को कैसे हल किया जाए, मेरे पास इस तरह के कई बिंदु हैं, तो समाधान जटिल नहीं होना चाहिए।कनवर्टर पैरामीटर को बस कैसे बांधें?

मेरे पास सेटिंग्स और मुख्य XAML के साथ मुख्य प्रोजेक्ट है।

मैं बाध्यकारी कनवर्टर और XAML फ़ाइल के साथ निर्भरता परियोजना है लगता है:

<TextBlock Text="{Binding X.Y.Z, Converter={StaticResource ProbabilityConverter}, ConverterParameter=??????????????, Mode=OneWay}"/> 

यह XAML फाइल मुख्य परियोजना से मुख्य XAML फाइल से लोड कर रहा है।

मैं ConverterParameter करने के स्थापना से एक संपत्ति के मूल्य से गुजरना होगा, इस पैरामीटर रनटाइम के दौरान बदलते जा सकता है, तो यह होना चाहिए Binding, Binding मैं केवल इस मामले में DependencyProperty के लिए कर सकते हैं।

मुझे इस समस्या को हल करने के लिए इस सेटिंग संपत्ति के लिए DependencyProperty रैपर करना होगा?

जब मैं सेट की कोशिश ConverterParameter में Binding मैं कार्यावधि में इस अपवाद मिल जाएगा:

ए 'बाध्यकारी' प्रकार की 'ConverterParameter' संपत्ति 'बाध्यकारी' पर सेट नहीं किया जा सकता है। एक 'बाइंडिंग' को निर्भरता ऑब्जेक्ट की निर्भरता प्रॉपर्टी पर सेट किया जा सकता है।

उत्तर

21

आप किसी भी संपत्ति से जुड़ सकते हैं, यह निर्भरता संपत्ति नहीं है।

  1. एक निर्भरता संपत्ति में संपत्ति बनाओ: लेकिन अगर आप संपत्ति तुरंत जब वे ऐसा में बदलाव को प्रतिबिंबित करने के लिए अपने यूआई चाहते हैं, आप दो विकल्प हैं।
  2. संपत्ति रखने वाले INotifyPropertyChanged को लागू करें और संपत्ति में परिवर्तन होने पर PropertyChanged ईवेंट बढ़ाएं।

संपादित करें:

के रूप में प्रश्न के संपादन में बताया, यह ConverterParameter करने के लिए बाध्य करने के लिए संभव नहीं है। लेकिन आप MultiBinding का उपयोग कर सकते हैं। उदाहरण के लिए, मान लीजिए कि आप किसी तारीख से जुड़ना चाहते हैं और कनवर्टर संस्कृति विनिर्देश को पैरामीटर के रूप में देना चाहते हैं और जब संस्कृति बदलती है तो बाध्यकारी को ताज़ा करें (मुझे यकीन नहीं है कि यह एक अच्छा विचार है, लेकिन यह एक उदाहरण के रूप में अच्छी तरह से कार्य करता है)। आप इसे इस तरह कर सकता है:

<TextBlock> 
    <TextBlock.Resources> 
     <local:DateCultureConverter x:Key="converter" /> 
    </TextBlock.Resources> 
    <TextBlock.Text> 
     <MultiBinding Converter="{StaticResource converter}"> 
      <Binding Path="Date" /> 
      <Binding Path="Settings.Culture" /> 
     </MultiBinding> 
    </TextBlock.Text> 
</TextBlock> 

यहाँ, दोनों Date और Settings वर्तमान DataContext पर गुण हैं। DateCultureConverterIMultiValueConverter लागू करता है और आप शायद इसे वास्तविक अनुप्रयोग में पदानुक्रम के कुछ स्तर संसाधनों में डाल देंगे।

+0

अगर मैं सामान्य संपत्ति के लिए बाध्य कर सकते हैं करेंगे तो मैं क्योंकि मैं inotitypropertychanged कार्यान्वयन के साथ वर्ग है ;-) लेकिन संकलक का कहना है समस्या नहीं होगी, तो मैं नेस्ट है बाइंडिंग तो आबद्ध करना होगा केवल निर्भरताप्रदर्शन के लिए ;-(मैं इसे फिर से करने की कोशिश करता हूं और इस विषय को अद्यतन करता हूं – Svisstack

+0

अपडेट किया गया, त्रुटि स्ट्रिंग जोड़ा गया! – Svisstack

+0

@Svisstack, अद्यतन उत्तर – svick

4

आप निम्न समाधानों में से एक का उपयोग कर सकते हैं:

  1. BindableParameter

https://marlongrech.wordpress.com/2008/08/03/my-wish-came-true-i-can-now-use-databinding-in-a-converterparameter/

आप वर्ग एकीकृत करना चाहिए (सामान्य बाध्यकारी + एक संलग्न संपत्ति और MarkupExtension का प्रयोग करके) बाइंडेबल पैरामीटर और बाइंडेबल पैरामीटर एक्सटेंशन (नीचे देखें) और फिर आप इसे निम्नानुसार उपयोग कर सकते हैं:

XAML में:

xmlns:local="clr-namespace:BindableParameterExtension" 

    <local:SampleConverter x:Key="sampleConverter" /> 

    <StackPanel Orientation="Vertical"> 
     <TextBox Name="txtContent" Text="Text from txtContent" /> 
     <TextBox Name="txtParameter" Text="Text from txtParameter" /> 
     <TextBox Name="txtBindingSample" 
       Text="{Binding ElementName=txtContent, Path=Text, Converter={StaticResource sampleConverter}}" 
       local:BindableParameter.BindParameter="{local:BindableParameter TargetProperty=TextBox.Text, 
           Binding={Binding ElementName=txtParameter, Path=Text} }" /> 
    </StackPanel> 

संपत्ति "TargetProperty":

TargetProperty=TextBox.Text 
BindableParamerter की

मूल बाध्य संपत्ति (इस मामले "TextBox.Text" में) करने के लिए सेट किया जाना चाहिए।

नमूना-कनवर्टर:

using System; 
using System.Windows.Data; 

namespace BindableParameterExtension 
{ 
    public class SampleConverter : IValueConverter 
    { 
     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      if (value != null && parameter != null) 
      { 
       return value.ToString() + ", " + parameter.ToString(); 
      } 
      return null; 
     } 

     public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      if (value is string && parameter is string) 
      { 
       string text1 = value as string; 
       string textParamter = parameter as string; 

       return text1.Replace(textParamter, ""); 
      } 
      return value; 
     } 
    } 
}  

पैरामीटर में "कन्वर्ट" इस्तेमाल किया जा सकता है और "ConvertBack" विधि (उपयोगी एक दृश्य मॉडल के लिए बाध्य करने के लिए)।

कक्षा BindableParameter और BindableParameterExtension (यूआरएल ऊपर देखें (नहीं मेरे कोड))

/* 
* Copyright - Everyone can use this code for any reason yet if you find a bug, I do not hold myself responsable :D 
*/ 

using System.Windows.Data; 
using System.Windows.Markup; 

namespace BindableParameterExtension 
{ 
    /// <summary> 
    /// BindableParameter is the class that changes the ConverterParameter Value 
    /// This must inherit from freezable so that it can be in the inheritance context and thus be able to use the DataContext and to specify ElementName binding as a ConverterParameter 
    /// http://www.drwpf.com/Blog/Default.aspx?tabid=36&EntryID=36 
    /// </summary> 
    public class BindableParameter : Freezable 
    { 
     #region fields 
     //this is a hack to trick the WPF platform in thining that the binding is not sealed yet and then change the value of the converter parameter 
     private static FieldInfo isSealedFieldInfo; 
     #endregion 

     #region Properties 
     #region Parameter 

     /// <summary> 
     /// Parameter Dependency Property 
     /// </summary> 
     public static readonly DependencyProperty ParameterProperty = 
      DependencyProperty.Register("Parameter", typeof(object), typeof(BindableParameter), 
       new FrameworkPropertyMetadata((object)null, 
        (d, e) => 
        { 
         BindableParameter param = (BindableParameter)d; 
         //set the ConverterParameterValue before calling invalidate because the invalidate uses that value to sett the converter paramter 
         param.ConverterParameterValue = e.NewValue; 
         //update the converter parameter 
         InvalidateBinding(param); 
        } 
        )); 

     /// <summary> 
     /// Gets or sets the Parameter property. This dependency property 
     /// indicates .... 
     /// </summary> 
     public object Parameter 
     { 
      get { return (object)GetValue(ParameterProperty); } 
      set { SetValue(ParameterProperty, value); } 
     } 

     #endregion 

     #region BindParameter 

     /// <summary> 
     /// BindParameter Attached Dependency Property 
     /// </summary> 
     public static readonly DependencyProperty BindParameterProperty = 
      DependencyProperty.RegisterAttached("BindParameter", typeof(BindableParameter), typeof(BindableParameter), 
       new FrameworkPropertyMetadata((BindableParameter)null, 
        new PropertyChangedCallback(OnBindParameterChanged))); 

     /// <summary> 
     /// Gets the BindParameter property. This dependency property 
     /// indicates .... 
     /// </summary> 
     public static BindableParameter GetBindParameter(DependencyObject d) 
     { 
      return (BindableParameter)d.GetValue(BindParameterProperty); 
     } 

     /// <summary> 
     /// Sets the BindParameter property. This dependency property 
     /// indicates .... 
     /// </summary> 
     public static void SetBindParameter(DependencyObject d, BindableParameter value) 
     { 
      d.SetValue(BindParameterProperty, value); 
     } 

     /// <summary> 
     /// Handles changes to the BindParameter property. 
     /// </summary> 
     private static void OnBindParameterChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
     { 
      FrameworkElement element = d as FrameworkElement; 
      if (element == null) 
       throw new InvalidOperationException("BindableParameter can be applied to a FrameworkElement only"); 

      BindableParameter parameter = (BindableParameter)e.NewValue; 
      element.Initialized += delegate 
      { 
       parameter.TargetExpression = BindingOperations.GetBindingExpression(element, parameter.TargetProperty); 
       parameter.TargetBinding = BindingOperations.GetBinding(element, parameter.TargetProperty); 

       //update the converter parameter 
       InvalidateBinding(parameter); 
      }; 
     } 

     #endregion 

     public object ConverterParameterValue { get; set; } 

     public BindingExpression TargetExpression { get; set; } 

     public Binding TargetBinding { get; private set; } 

     /// <summary> 
     /// Gets the object being bound 
     /// </summary> 
     public DependencyObject TargetObject { get; private set; } 

     /// <summary> 
     /// Gets the dependency property being bound 
     /// </summary> 
     public DependencyProperty TargetProperty { get; internal set; } 
     #endregion 

     /// <summary> 
     /// Static constructor to get the FieldInfo meta data for the _isSealed field of the BindingBase class 
     /// </summary> 
     static BindableParameter() 
     { 
      //initialize the field info once 
      isSealedFieldInfo = 
       typeof(BindingBase).GetField("_isSealed", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); 

      if (isSealedFieldInfo == null) 
       throw new InvalidOperationException("Oops, we have a problem, it seems like the WPF team decided to change the name of the _isSealed field of the BindingBase class."); 

     } 

     private static void InvalidateBinding(BindableParameter param) 
     { 
      if (param.TargetBinding != null && param.TargetExpression != null) 
      { 
       //this is a hack to trick the WPF platform in thining that the binding is not sealed yet and then change the value of the converter parameter 
       bool isSealed = (bool)isSealedFieldInfo.GetValue(param.TargetBinding); 

       if (isSealed)//change the is sealed value 
        isSealedFieldInfo.SetValue(param.TargetBinding, false); 

       param.TargetBinding.ConverterParameter = param.ConverterParameterValue; 

       if (isSealed)//put the is sealed value back as it was... 
        isSealedFieldInfo.SetValue(param.TargetBinding, true); 

       //force an update to the binding 
       param.TargetExpression.UpdateTarget(); 
      } 
     } 

     #region Freezable Stuff 
     protected override Freezable CreateInstanceCore() 
     { 
      //throw new NotImplementedException(); 
      //return _bindableParam; 

      return this; 
     } 
     #endregion 

    } 

    /// <summary> 
    /// Markup extension so that it is easier to create an instance of the BindableParameter from XAML 
    /// </summary> 
    [MarkupExtensionReturnType(typeof(BindableParameter))] 
    public class BindableParameterExtension : MarkupExtension 
    { 
     /// <summary> 
     /// Gets or sets the Dependency property you want to change the binding's ConverterParameter 
     /// </summary> 
     public DependencyProperty TargetProperty { get; set; } 

     /// <summary> 
     /// Gets or sets the Binding that you want to use for the converter parameter 
     /// </summary> 
     public Binding Binding { get; set; } 

     /// <summary> 
     /// constructor that accepts a Dependency Property so that you do not need to specify TargetProperty 
     /// </summary> 
     /// <param name="property">The Dependency property you want to change the binding's ConverterParameter</param> 
     public BindableParameterExtension(DependencyProperty property) 
     { 
      TargetProperty = property; 
     } 

     public BindableParameterExtension() 
     { } 

     public override object ProvideValue(IServiceProvider serviceProvider) 
     { 
      _bindableParam = new BindableParameter(); 
      //set the binding of the parameter 
      BindingOperations.SetBinding(_bindableParam, BindableParameter.ParameterProperty, Binding); 
      _bindableParam.TargetProperty = TargetProperty; 
      return _bindableParam; 
     } 

     private BindableParameter _bindableParam; 

    } 
} 
  1. ObjectReference:

http://drwpf.com/blog/2007/09/02/supplying-an-object-reference-in-the-constructorparameters-collection-of-an-objectdataprovider/

आप कक्षा ऑब्जेक्ट रेफरेंस को एकीकृत करना होगा:

http://www.drwpf.com/blog/Portals/0/Code/ObjectReference.cs.txt

XAML में:

xmlns:local="clr-namespace:WpfMarkupExtension" 

<local:SampleConverter x:Key="sampleConverter" /> 

<StackPanel Orientation="Vertical"> 
    <TextBox Name="txtContent" Text="Text from txtContent" /> 
    <TextBox Name="txtParameter" Text="Text from txtParameter" local:ObjectReference.Declaration="{local:ObjectReference txtParam}" /> 
    <TextBox Name="txtBindingSample" 
      Text="{Binding ElementName=txtContent, Path=Text, 
          Converter={StaticResource sampleConverter}, 
          ConverterParameter={local:ObjectReference txtParam}}" /> 
</StackPanel> 

कतरना:

ConverterParameter={local:ObjectReference txtParam}}" 

से इस वस्तु संदर्भ लेता है:

local:ObjectReference.Declaration="{local:ObjectReference txtParam}" 

एक स्थिर शब्दकोश में संदर्भ और हिस्सा बनाता है शब्दकोश -> यहां कोई बाध्यकारी नहीं है, शब्दकोश फाई है पार्स समय पर लेट गया।

नमूना-कनवर्टर:

using System; 
using System.Windows.Controls; 
using System.Windows.Data; 

namespace WpfMarkupExtension 
{ 
    public class SampleConverter : IValueConverter 
    { 
     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      if (value != null && parameter is TextBox) 
      { 
       return value.ToString() + ", " + ((TextBox)parameter).Text; 
      } 
      return null; 
     } 

     public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      if (value is string && parameter is TextBox) 
      { 
       string text1 = value as string; 
       string textParamter = ((TextBox)parameter).Text; 

       return text1.Replace(textParamter, ""); 

      } 

      return value; 
     } 
    } 
} 
  1. Bindable कनवर्टर पैरामीटर (कस्टम बाइंडिंग सिंटैक्स का उपयोग):

http://www.codeproject.com/Articles/456589/Bindable-Converter-Parameter

XAML में :

xmlns:local="clr-namespace:BcpBindingExtension" 

    <local:SampleConverter x:Key="sampleConverter" /> 

    <StackPanel Orientation="Vertical"> 
     <TextBox Name="txtContent" Text="Text from txtContent" /> 
     <TextBox Name="txtParameter" Text="Text from txtParameter" /> 
     <TextBox Name="txtBindingSample"> 
       <TextBox.Text> 
        <local:BcpBinding Path="Text" ElementName="txtContent" 
             Converter="{StaticResource sampleConverter}" 
             ConverterParameters="Binding Path=Text ElementName=txtParameter" 
             Mode="TwoWay"/> 
       </TextBox.Text> 
     </TextBox> 
    </StackPanel> 

नमूना-कनवर्टर:

using System; 
using System.Windows.Data; 

namespace BcpBindingExtension 
{ 
    public class SampleConverter : IValueConverter 
    { 
     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      if (value != null && parameter is object[] && ((object[])parameter).Length > 0) 
      { 
       return value.ToString() + ", " + ((object[])parameter)[0].ToString(); 
      } 
      return null; 
     } 

     public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      if (value is string && parameter is object[] && ((object[])parameter).Length > 0) 
      { 
       string text1 = value as string; 
       string textParamter = ((object[])parameter)[0] as string; 

       return text1.Replace(textParamter, ""); 

      } 

      return value; 
     } 
    } 
} 
संबंधित मुद्दे