如何覆盖DataGrid中从XML生成的超链接字符串值?

时间:2011-01-20 20:17:31

标签: c# wpf wpf-controls wpfdatagrid

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

链接到测试解决方案: 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();
    }
}

OptionsWindow XAML with RadioButtons和OK按钮:

<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>

1 个答案:

答案 0 :(得分:1)

我不确定我是否完全明白你的问题,但无论如何我还是捅了一下。您可以使用CommandParameter通过Command发送参数。如果您使用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;
        }
    }
}