从XAML调用C#函数
在我的XAML中,我有一个这样的扩展器从XAML调用C#函数,c#,wpf,xaml,C#,Wpf,Xaml,在我的XAML中,我有一个这样的扩展器 <Expander ExpandDirection="Down" Width="Auto" Padding="4"> <Expander.Style> <Style TargetType="Expander"> <Setter Property="IsExpanded" Value="{Binding XPath=@Expand}" />
<Expander ExpandDirection="Down" Width="Auto" Padding="4">
<Expander.Style>
<Style TargetType="Expander">
<Setter Property="IsExpanded" Value="{Binding XPath=@Expand}" />
<Setter Property="Header" Value="{Binding XPath=@Name}" />
<Setter Property="FontWeight" Value="Bold" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsExpanded,RelativeSource={RelativeSource Self}}" Value="True">
</DataTrigger>
</Style.Triggers>
</Style>
</Expander.Style>
<ListBox Name="itemsList"
ItemsSource="{Binding XPath=UpgradeAction}"
ItemTemplate="{StaticResource dtListItemTemplate}"
SelectionChanged="listItems_SelectionChanged"
Style="{StaticResource styleListBoxUpgradeAction}"
ItemContainerStyle="{StaticResource styleListBoxItemUpgradeAction}">
</ListBox>
</Expander>
<Setter Property="Header" Value="C_Sharp_Function({Binding XPath=@Name})" />
我想用一个C#函数从XAML中接收@Name,而不是@Name中的Header值,做一些事情,然后输出一个新字符串作为值。看起来是这样的
<Expander ExpandDirection="Down" Width="Auto" Padding="4">
<Expander.Style>
<Style TargetType="Expander">
<Setter Property="IsExpanded" Value="{Binding XPath=@Expand}" />
<Setter Property="Header" Value="{Binding XPath=@Name}" />
<Setter Property="FontWeight" Value="Bold" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsExpanded,RelativeSource={RelativeSource Self}}" Value="True">
</DataTrigger>
</Style.Triggers>
</Style>
</Expander.Style>
<ListBox Name="itemsList"
ItemsSource="{Binding XPath=UpgradeAction}"
ItemTemplate="{StaticResource dtListItemTemplate}"
SelectionChanged="listItems_SelectionChanged"
Style="{StaticResource styleListBoxUpgradeAction}"
ItemContainerStyle="{StaticResource styleListBoxItemUpgradeAction}">
</ListBox>
</Expander>
<Setter Property="Header" Value="C_Sharp_Function({Binding XPath=@Name})" />
我有我需要使用的函数,但是我如何在XAML中执行它?如果这听起来像个愚蠢的问题,很抱歉,我对C#和XAML是新手。非常感谢您的帮助
编辑
下面是我在Setter中尝试的一些东西,以替代多值转换器。我想它应该看起来像中的答案
您所描述的是一个问题。看起来有点不同,但逻辑上是一样的
public class NameConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var name = (XmlAttribute)value;
// Do anything you like in here. This is just an example.
return "something based on " + name.Value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
XAML:
你所描述的是一个例子。看起来有点不同,但逻辑上是一样的
public class NameConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var name = (XmlAttribute)value;
// Do anything you like in here. This is just an example.
return "something based on " + name.Value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
XAML:
绑定到viewmodel上的属性,并在属性的getter中放入函数中的代码我有点同意@Milney,为什么不在viewmodel上使用一个属性,该属性返回正确的标头值呢?为什么它需要做这样的往返呢?但这就是转换器所做的。因此,在绑定中添加一个转换器,并在其中执行您需要的操作。这是否回答了您的问题?绑定到viewmodel上的属性,并在属性的getter中放入函数中的代码我有点同意@Milney,为什么不在viewmodel上使用一个属性,该属性返回正确的标头值呢?为什么它需要做这样的往返呢?但这就是转换器所做的。因此,在绑定中添加一个转换器,并在其中执行您需要的操作。这是否回答了您的问题?谢谢你的回复。我的一个问题是XAML中的窗口到底在哪里。资源应该放在哪里?它也应该包裹扩展器吗?最后,我得到了错误“在类型'Window'中找不到可附加属性'Resources'。我应该为此包含命名空间吗?@JohnSmitty我无法诊断我没有看到的XAML,但我已更新了答案。如果您仍然有问题,请将新的XAML添加到您的答案中。除了一件事之外,这似乎有效。在您创建的转换中,我在(String)值上收到错误“无法将'System.Xml.xmldattribute'类型的对象强制转换为'System.String'类型”。我尝试将其更改为value.ToString(),但输出的是System.Xml.XmlAttribute而不是实际字符串。在调试时,我可以在变量“value”中看到一个parm值,只是不知道如何获取它。有什么想法吗?将其转换为XmlAttribute而不是字符串,并获取值属性。我阅读了有关如何实现多值转换器的链接,我的C#代码很好,但我将如何在XAML中实现它?感谢您的回复。我的一个问题是XAML中的窗口到底在哪里。资源应该放在哪里?它也应该包裹扩展器吗?最后,我得到了错误“在类型'Window'中找不到可附加属性'Resources'。我应该为此包含命名空间吗?@JohnSmitty我无法诊断我没有看到的XAML,但我已更新了答案。如果您仍然有问题,请将新的XAML添加到您的答案中。除了一件事之外,这似乎有效。在您创建的转换中,我在(String)值上收到错误“无法将'System.Xml.xmldattribute'类型的对象强制转换为'System.String'类型”。我尝试将其更改为value.ToString(),但输出的是System.Xml.XmlAttribute而不是实际字符串。在调试时,我可以在变量“value”中看到一个parm值,只是不知道如何获取它。有什么想法吗?将其转换为XmlAttribute而不是字符串,并获取值属性我阅读了关于如何实现多值转换器的链接,我的C代码很好,但我如何在XAML中实现它呢?
public class NameValueConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
var attrName = (XmlAttribute)values[0];
var attrValue = (XmlAttribute)values[1];
// Do stuff here.
return attrName.Value + " -> " + attrValue.Value;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
<Window.Resources>
<local:NameValueConverter x:Key="NameValueConverter" />
</Window.Resources>
<Expander
ExpandDirection="Down"
Width="Auto"
Padding="4"
IsExpanded="{Binding XPath=@Expand}"
>
<Expander.Style>
<Style TargetType="Expander">
<Setter Property="FontWeight" Value="Bold" />
<Style.Triggers>
<!--
You can do a regular Trigger instead of a DataTrigger and not
have to bother with RelativeSource=Self
-->
<Trigger
Property="IsExpanded"
Value="True">
<!-- stuff -->
</Trigger>
</Style.Triggers>
</Style>
</Expander.Style>
<Expander.Header>
<MultiBinding Converter="{StaticResource NameValueConverter}">
<Binding XPath="@Name" />
<Binding XPath="@Value" />
</MultiBinding>
</Expander.Header>
</Expander>
<Window
...x:Class="..." and other attributes...
>
<Window.Resources>
<local:NameConverter x:Key="NameConverter" />
<!-- other resources -->
</Window.Resources>
<Grid>
<!-- The controls that go in the window -->
</Grid>
</Window