【问题标题】:How to override the hyperlink string value generated from XML in DataGrid?如何覆盖 DataGrid 中 XML 生成的超链接字符串值?
【发布时间】:2011-01-20 20:17:31
【问题描述】:

我有一个场景,我需要在检查驻留在单独的“选项”窗口中的 RadioButton 后更改数据网格中超链接的名称。我使用分配给同一超链接的 ICommand 启动此“选项”窗口。我无法重命名超链接名称。超链接是使用 XMLDataProvider 从 XML 生成的。我还使用将 XML 字符串转换为 ICommand 的 IValueConverter。我希望在这里找到解决方案。先感谢您。

测试解决方案链接: http://cid-0c29483cf3a6a14d.office.live.com/self.aspx/WPF%5E_Tests/RenameHyperlinkText.zip

代码如下。

在主窗口中生成带有超链接 XAML 的 dataGrid:

<Window.Resources>
    <local:MyStringToCommandConverter x:Key="MyStringToCommandConverter"/>
    <XmlDataProvider x:Key="MainDataGridLocal" XPath="ServicesTiles/Servers">
        <x:XData>
            <ServicesTiles xmlns="">
              <Servers Name="Name 1" Status="None" Name2="Name 2" Status2="Never" Command="LaunchOptionsWindowCommand" />
              <Servers Name="Name 3" Status="none"  Name2="Name 4" Status2="None" />
            </ServicesTiles>
        </x:XData>
    </XmlDataProvider>
</Window.Resources> 
<Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource MainDataGridLocal}}">
    <DataGrid x:Name="MainGrid" AutoGenerateColumns="False" ItemsSource="{Binding XPath=/ServicesTiles/Servers}">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding XPath=@Name}" />  
            <DataGridTemplateColumn >     
                <DataGridTemplateColumn.CellTemplate>         
                    <DataTemplate>             
                        <TextBlock >
                            <Hyperlink Command="{Binding XPath=@Command, Converter={StaticResource MyStringToCommandConverter}}" >                
                                    <TextBlock Text="{Binding XPath=@Status}" />
                            </Hyperlink>
                        </TextBlock>         
                    </DataTemplate>     
                </DataGridTemplateColumn.CellTemplate>                          
            </DataGridTemplateColumn> 
            <DataGridTextColumn Binding="{Binding XPath=@Status2}"/>
            <DataGridTemplateColumn >     
                <DataGridTemplateColumn.CellTemplate>         
                    <DataTemplate>             
                        <TextBlock >
                            <Hyperlink >                   
                                <TextBlock Text="{Binding XPath=@Status2}" />
                            </Hyperlink>
                        </TextBlock>         
                    </DataTemplate>     
                </DataGridTemplateColumn.CellTemplate>                          
            </DataGridTemplateColumn> 
        </DataGrid.Columns>
    </DataGrid>
</Grid>

运行命令的代码:

public partial class MainWindow : Window
{

public static RoutedUICommand OptionsWindowCommand = new RoutedUICommand("Options...", "Options", typeof(MainWindow));

    public MainWindow()
    {
        this.InitializeComponent();
        this.CommandBindings.Add(new CommandBinding(OptionsWindowCommand, OptionsWindowCommandExecuted));
    }
    private void OptionsWindowCommandExecuted(object sender, ExecutedRoutedEventArgs e)
    {
        OptionsWindow theDialog = new OptionsWindow();
        if ( theDialog != null )
            theDialog.ShowDialog();
    }
}

带有 RadioButtons 和 OK 按钮的 OptionsWindow XAML:

<Grid x:Name="LayoutRoot" Height="250" VerticalAlignment="Top">
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Button Content="Cancel" VerticalAlignment="Bottom" Margin="0,0,20,0" OpacityMask="#FFE0DADA" IsCancel="True" HorizontalAlignment="Right" Width="75"/>
    <Button Content="OK" HorizontalAlignment="Right" VerticalAlignment="Bottom" Width="75" Margin="0,0,103.06,0" IsDefault="True" />
    <StackPanel HorizontalAlignment="Center"  Width="158" VerticalAlignment="Center">
        <RadioButton x:Name="a_RD" Content="1_RD" Height="25"/>
        <RadioButton x:Name="b_RD" Content="2_RD;" IsChecked="True" Height="25"/>
        <RadioButton x:Name="c_RD" Content="3_RD" Height="25"/>
    </StackPanel>
</Grid>

【问题讨论】:

  • 我查看了您的代码,但不确定我是否理解您想要做什么。启动对话框后,选择一个值,然后要更改“超链接的名称”。这是否意味着您想根据您在对话框中选择的内容将“名称”的值更改为某个值?名称是否与 &lt;DataGridTextColumn Binding="{Binding XPath=@Name}" /&gt; 一起显示?
  • 我想将 'Status' 节点属性的字符串更改为 'Available' 而不是 'None'。抱歉解释不佳。如果现在清楚请告诉我。
  • 是的,您理解正确。当您选择 RadioButton 然后单击“确定”按钮时,应该会发生更改。再次感谢您。
  • 更新了我的答案,这和你要找的很接近吗?

标签: c# wpf wpf-controls wpfdatagrid


【解决方案1】:

我不确定我是否完全理解了你的问题,但我还是试了一下。您可以使用CommandParameter 通过命令发送参数。如果您使用CommandParameter="{Binding}",您将获得点击行对应的XmlLinkedNode(即DataContext),您可以从那里访问其中的属性。

<DataGridTemplateColumn >     
    <DataGridTemplateColumn.CellTemplate>         
        <DataTemplate>             
            <TextBlock >
                <Hyperlink Command="{Binding XPath=@Command, Converter={StaticResource MyStringToCommandConverter}}"
                           CommandParameter="{Binding}">
                    <TextBlock Text="{Binding XPath=@Status}" />
                </Hyperlink>
            </TextBlock>
        </DataTemplate>     
    </DataGridTemplateColumn.CellTemplate>                          
</DataGridTemplateColumn>

然后你可以在OptionsWindowCommandExecuted 中做这样的事情。我可能会补充一点,我不太熟悉使用XmlDataProvider,所以Attributes["Status"].Value 可能不是推荐的方式:)

private void OptionsWindowCommandExecuted(object sender, ExecutedRoutedEventArgs e)
{
    OptionsWindow theDialog = new OptionsWindow();
    XmlLinkedNode xmlLinkedNode = e.Parameter as XmlLinkedNode;
    if (theDialog != null)
    {
        if (theDialog.ShowDialog() == true)
        {
            xmlLinkedNode.Attributes["Status"].Value = "Available";
        }
    }
}

更新
一些示例代码根据用户检查的RadioButton 设置不同的值

在 OptionsWindow.xaml.cs 中

public string CheckedRadioButtonContent
{
    get;
    set;
}

private void OKButton_Click(object sender, RoutedEventArgs e)
{
    if (a_RD.IsChecked == true)
    {
        CheckedRadioButtonContent = a_RD.Content.ToString();
    }
    else if (b_RD.IsChecked == true)
    {
        CheckedRadioButtonContent = b_RD.Content.ToString();
    }
    else if (c_RD.IsChecked == true)
    {
        CheckedRadioButtonContent = c_RD.Content.ToString();
    }
    DialogResult = true;
}

MainWindow.xaml.cs

private void OptionsWindowCommandExecuted(object sender, ExecutedRoutedEventArgs e)
{
    OptionsWindow theDialog = new OptionsWindow();
    XmlLinkedNode xmlLinkedNode = e.Parameter as XmlLinkedNode;
    if (theDialog != null)
    {
        if (theDialog.ShowDialog() == true)
        {
            string checkedRadioButtonContent = theDialog.CheckedRadioButtonContent;
            xmlLinkedNode.Attributes["Status"].Value = "Available_" + checkedRadioButtonContent;
        }
    }
}

【讨论】:

  • 我有 3 个单选按钮。是否可以根据选中的单选按钮更改名称?您是否愿意为其提供示例代码。再次感谢您。
  • @vladc77:为此添加了一些示例代码,这是您的意思吗?
  • 谢谢!我有个问题。它不再重命名。
  • 是的,我的意思正是您在代码中添加的内容。不幸的是,它根本没有重命名属性。我正在寻找任何错误,但还没有找到。
  • @vladc77:我上传了我的示例应用程序。将其与您的比较以查看差异:mediafire.com/?v486v18lcxi2s49
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-17
  • 2010-12-30
  • 2011-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多