如何将类型传递给 UWP 中的 ConverterParameter?
How to pass a Type into ConverterParameter in UWP?
既然你不能在 UWP 中使用 x:type,你如何将对象的类型作为转换器参数传递给转换器?基本上,我想在 UWP 中这样做:
Header="{x:Bind NavMenu.SelectedItem,
Mode=OneWay,
Converter={StaticResource ObjectToStringConverter},
ConverterParameter={x:type NavigationViewItem}}"
是的,我意识到因为我正在使用 x:Bind 我可以直接转换对象。但是,Resharper 抱怨转换并将其标记为语法错误,因此我想通过使用转换器来解决它。另外,只是因为我很好奇您如何将 Type 传递到 XAML 中的转换器以用于其他用途。
How to pass a Type into ConverterParameter in UWP?
您不能直接在 ConverterParameter
上绑定一个值,原因是 ConverterParameter
不是一个依赖项 属性,而是一个 简单的 对象。在这种情况下,您不能使用绑定并传递 x:type
是错误的语法。
根据您的要求,您可以在转换器上创建依赖项 属性 而不是使用 ConverterParameter
:为了能够创建 DP,您的转换器必须继承自 DependencyObject
.详细步骤可以参考这篇博客 WinRT Converter Parameter Binding
更新
如果你想传递Type参数,你可以使用get type Functions in x:Bind。
例如:
static public class MyHelpers
{
public static Type GetType(object ele)
{
return ele.GetType();
}
}
Xaml
<TextBox Text="{x:Bind local:MyHelpers.GetType(Item)}" />
既然你不能在 UWP 中使用 x:type,你如何将对象的类型作为转换器参数传递给转换器?基本上,我想在 UWP 中这样做:
Header="{x:Bind NavMenu.SelectedItem,
Mode=OneWay,
Converter={StaticResource ObjectToStringConverter},
ConverterParameter={x:type NavigationViewItem}}"
是的,我意识到因为我正在使用 x:Bind 我可以直接转换对象。但是,Resharper 抱怨转换并将其标记为语法错误,因此我想通过使用转换器来解决它。另外,只是因为我很好奇您如何将 Type 传递到 XAML 中的转换器以用于其他用途。
How to pass a Type into ConverterParameter in UWP?
您不能直接在 ConverterParameter
上绑定一个值,原因是 ConverterParameter
不是一个依赖项 属性,而是一个 简单的 对象。在这种情况下,您不能使用绑定并传递 x:type
是错误的语法。
根据您的要求,您可以在转换器上创建依赖项 属性 而不是使用 ConverterParameter
:为了能够创建 DP,您的转换器必须继承自 DependencyObject
.详细步骤可以参考这篇博客 WinRT Converter Parameter Binding
更新
如果你想传递Type参数,你可以使用get type Functions in x:Bind。
例如:
static public class MyHelpers
{
public static Type GetType(object ele)
{
return ele.GetType();
}
}
Xaml
<TextBox Text="{x:Bind local:MyHelpers.GetType(Item)}" />