引用当前类 xaml Windows Phone



我得到了以下代码:

namespace SomeApp
{
  public partial class MyClass : PhoneApplicationPage, IValueConverter
  {
    SOME METHODS...
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return true;
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return true;
    }
  }
}


我想将这个类绑定到一个单选按钮的价值转换器。有什么方法可以引用我正在使用的当前类吗?例如:

<phone:PhoneApplicationPage
x:Class="SomeApp.MyClass"
xmlns:local="clr-namespace:SomeApp">

<phone:PhoneApplicationPage.Resources>
<local:MyClass x:Key="myClass"/>
</phone:PhoneApplicationPage.Resources>

<RadioButton IsChecked="{Binding Converter={StaticResource myClass}}"/>

提前感谢=)

首先使用您的页面作为转换器似乎不是一个好主意,最好将转换器功能分离到一个单独的类中。特别是,以这种方式创建的转换器的静态资源将是一个非常糟糕的主意,因为它将使用大量内存来创建整个页面。

在 xaml 中唯一可以绑定转换器的是 StaticResource,因此您将无法在 xaml 中执行此操作,但如果您真的想这样做,您可以通过从代码隐藏创建绑定来实现(例如在页面的构造函数中):

Binding binding=new Binding();
binding.Converter = this;
myRadioButton.SetBinding(CheckBox.IsCheckedProperty, binding);

最新更新