如何将 XML 中的字符串转换为返回 ICommand?

发布于 2024-10-13 02:13:14 字数 1704 浏览 6 评论 0原文

我需要能够将唯一的命令传递给从 DataGrid 中的 XML 生成的超链接。

如果我以这种方式将它们直接指向超链接,那么我在该工作背后的代码中有命令。

<Hyperlink Style="{DynamicResource DataGridCellStyleHyperlink}" Command="{x:Static local:MainWindow.LaunchFirstCommand}">   

我需要做类似的事情,但动态地为单元格内的每个超链接分配不同的命令。所有超链接都是从 XML 生成的。我相信我需要某种转换器来做到这一点。我很难让它发挥作用。任何建议都将受到高度赞赏。先感谢您。

下面是在 DataGrid 中生成内容的 XMLDataProdider 代码。我尝试将“命令”值作为字符串传递:

<XmlDataProvider x:Key="MoreInfoDataGridLocal" XPath="MoreInfoTiles/Servers">
       <x:XData> 
        <MoreInfoTiles xmlns="">
         <Servers Name="Test1" Status="003" Name2="Connection 2" Status2="assigned" />
             <Servers Name="Test2" Status="Not activated" Name2="Address" Status2="test" />
             <Servers Name="Test3" Status="Disabled" Name2="Address" Status2="None" Command="x:Static local:MainWindow.LaunchFirstCommand"/>
        </MoreInfoTiles>
       </x:XData>
  </XmlDataProvider>

我可以成功生成文本字符串,但命令没有执行任何操作。下面是我将其连接到数据网格中的超链接的代码:

<DataGridTemplateColumn>   
<DataGridTemplateColumn.CellTemplate>     
    <DataTemplate>       
        <TextBlock >         
            <Hyperlink Style="{DynamicResource DataGridCellStyleHyperlink}" Command="{Binding XPath=@Command}" >           
                <TextBlock Text="{Binding XPath=@Status}" />                                                   
            </Hyperlink>                         
        </TextBlock>     
    </DataTemplate>   
    </DataGridTemplateColumn.CellTemplate>             
</DataGridTemplateColumn>

I need to be able to pass an unique commands to the hyperlinks that generated from XML in a DataGrid.

I have commands in the code behind that work if I point them directly to the hyperlinks this way.

<Hyperlink Style="{DynamicResource DataGridCellStyleHyperlink}" Command="{x:Static local:MainWindow.LaunchFirstCommand}">   

I need to do similar but assigning different commands to each hyperlink inside cells dynamically. All hyperlinks are generated from XML. I belive I need to have some sort of converter that will do it. I have trouble making it work. Any advice is highly appreciated. Thank you in advance.

Here is XMLDataProdider code that generates content inside DataGrid. I tried to pass 'Command' value as a string:

<XmlDataProvider x:Key="MoreInfoDataGridLocal" XPath="MoreInfoTiles/Servers">
       <x:XData> 
        <MoreInfoTiles xmlns="">
         <Servers Name="Test1" Status="003" Name2="Connection 2" Status2="assigned" />
             <Servers Name="Test2" Status="Not activated" Name2="Address" Status2="test" />
             <Servers Name="Test3" Status="Disabled" Name2="Address" Status2="None" Command="x:Static local:MainWindow.LaunchFirstCommand"/>
        </MoreInfoTiles>
       </x:XData>
  </XmlDataProvider>

I can successfully generate the text strings but command is not doing anything. Below is the code where I hook it up to the hyperlink in datagrid:

<DataGridTemplateColumn>   
<DataGridTemplateColumn.CellTemplate>     
    <DataTemplate>       
        <TextBlock >         
            <Hyperlink Style="{DynamicResource DataGridCellStyleHyperlink}" Command="{Binding XPath=@Command}" >           
                <TextBlock Text="{Binding XPath=@Status}" />                                                   
            </Hyperlink>                         
        </TextBlock>     
    </DataTemplate>   
    </DataGridTemplateColumn.CellTemplate>             
</DataGridTemplateColumn>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巨坚强 2024-10-20 02:13:14

是的,您需要使用 IValueConverter 将字符串转换为命令对象。您的命令绑定将如下所示:

Command="{Binding XPath=@Command, Converter={StaticResource MyStringToCommandConverter}}"

并且您将需要创建一个作为资源的转换器实例:

<MyStringToCommandConverter x:Key="MyStringToCommandConverter"/>

除此之外,您只需要创建实现 IValueConverter 的 MyStringToCommandConverter (或任何您命名的)类,并在 Convert 方法中翻译“ value”字符串到您的路由命令之一。一个简单的转换器看起来像这样:

public class MyStringToCommandConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string commandType = value as String;
        if (commandType == "LaunchFirstCommand")
            return MainWindow.LaunchFirstCommand;
        if (commandType == "OtherCommand")
            return MainWindow.OtherCommand;
        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new System.NotImplementedException();
    }
}

Yes, you will need to use an IValueConverter to translate the string to a command object. Your Command Binding will look like this:

Command="{Binding XPath=@Command, Converter={StaticResource MyStringToCommandConverter}}"

and you will need an instance of the converter created as a resource:

<MyStringToCommandConverter x:Key="MyStringToCommandConverter"/>

Other than that you just need to create the MyStringToCommandConverter (or whatever you name it) class implementing IValueConverter and in the Convert method translate the "value" string into one of your Routed Commands. A simple converter would look something like this:

public class MyStringToCommandConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string commandType = value as String;
        if (commandType == "LaunchFirstCommand")
            return MainWindow.LaunchFirstCommand;
        if (commandType == "OtherCommand")
            return MainWindow.OtherCommand;
        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new System.NotImplementedException();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文