簡體   English   中英

使用Binding作為ConverterParameter

[英]Use Binding as ConverterParameter

我正在嘗試使用值綁定作為轉換器參數,如下面的代碼片段所示:

<Element
  Attribute="{Binding Value,
              Converter={StaticResource EqualityConverter},
              ConverterParameter={Binding CompareTo}}" />

問題是,使用Binding實例作為轉換器參數( CompareTo )調用EqualityConverter::Convert()方法,而不是綁定的具體值。

有更聰明的方法來解決它嗎? 我顯然可以在我的視圖模型中提供轉換后的屬性,但我想知道是否有類似的工作解決方案。

另一種可能更簡單的方法是在轉換器本身上定義一個可綁定屬性。

public class CompareConverter: IValueConverter, INotifyPropertyChanged{
  private ComparisonType _comparison = ComparisonType.Equals;
  public ComparisonType Comparison {
    get {return _comparison; }
    set { _comparison = value; OnPropertyChanged(); }
  }

  private string _compareTo = null;
  public string CompareTo {
    get {return _compareTo; }
    set { _compareTo = value; OnPropertyChanged(); }
  }

  public event PropertyChangedEventHandler PropertyChanged;

  protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  { 
    if (PropertyChanged != null) 
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
  } 

  public object Convert (object value, Type targetType, object parameter, 
     System.Globalization.CultureInfo culture)
  {
    bool result = false;
    switch (Comparison)...
    return result;
  } 
  ...
}

您也可以通過繼承BindableObject並實現可綁定屬性來完成此操作,如果綁定上下文未被攜帶到資源中,您可能需要這樣做。 如果是這種情況,您可以在構造函數中從代碼后面設置一次,在調用Initialize方法之后。

你的xaml看起來像這樣:

..
<ResourceDictionary>
  <myPrefix:CompareConverter x:Key="compareToY" Comparison="Equals" CompareTo="{Binding... }"/>
</ResourceDictionary>
...
<Control Value="{Binding X, Converter={StaticResource compareToY}}"/>

可能需要一些調整,結果應該比多綁定更干凈

我正在努力解決同樣的問題而沒有得到我想要的數據。 你不能綁定到“converterParameter”,這就是它在當前日期的制作方式。 但我真的想得到一些發送到參數的數據。 所以我找到了一個簡單但有效的解決方案,我希望它也適合你。 首先給出CompareTo ax:Name =“CompareTo”或者你想要的任何東西。

         <Element
          Attribute="{Binding Value,
          Converter={StaticResource EqualityConverter},
          ConverterParameter={x:reference CompareTo}}" />

通過x:引用它實際上現在發送一些數據,你只需抓住它。 對我來說,我需要的值是“字符串”值,以便我可以做某些“如果”語句。 所以你可以做類似的事情:

    if(CompareTo == "myCoolString")
    {
      Value = "Well i guess i'm not so cool!"
    }

這是我從參數中獲取數據的方式:

   public class MyConverter : IValueConverter
    {

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (parameter != null)
        {
            try
            {
                var image = parameter as Image;
                var src = image.Source as FileImageSource;

                if (src.File.ToString() == "coolimage.png")
                {
                    return "thumbsup.png";
                }
            }
            catch
            {
            }
        } 
    }

在我的情況下,我正在使用和圖像,並需要知道一個圖像是“A”,然后“B”需要轉換為圖像“C”。 這應該與其他對象一起使用。 運氣不錯,這會讓你更接近一種簡單的“多重綁定”和回答。

希望這是我在Stackoverflow上的第一篇文章有​​用!

ConverterParameter不是依賴屬性,因此您將無法綁定任何屬性。 但是,您可以嘗試將MultiBinding與MultiValueConverter一起使用。

碼:

<TextBox>
    <TextBox.Text>
        <MultiBinding Converter="{StaticResource MultiValueConverter}">
            <Binding Path="property1"/>
            <Binding Path="property2"/>
        </MultiBinding>
    </TextBox.Text>
</TextBox>
public class MultiValueConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, 
           object parameter, System.Globalization.CultureInfo culture)
    {
         property1 = values[0];
         property2 = values[1];
         // Do your logic with the properties here.
    }
    public object[] ConvertBack(object value, Type[] targetTypes, 
           object parameter, System.Globalization.CultureInfo culture)
    {

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM