I have a scenario where I need to change the name of the hyperlink in datagrid after checking a RadioButton that resides in separate “Options” window. I launch this “Option” Window using ICommand that assigned to the same hyperlink. I have trouble renaming the hyperlink name. The hyperlinks are generated from XML using XMLDataProvider. I also use IValueConverter that converts XML string to ICommand. I hope to find solution here. Thank you in advance.
Link to the testing solution: http://cid-0c29483cf3a6a14d.office.live.com/self.aspx/WPF%5E_Tests/RenameHyperlinkText.zip
Code below.
Generating dataGrid with hyperlinks XAML in Main Window:
<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>
Code behind to run command:
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 and OK button:
<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>
I'm not sure I fully understood your question but I took a stab at it anyway. You can use CommandParameter
to send a parameter with the Command. If you use CommandParameter="{Binding}"
you'll get the corresponding XmlLinkedNode
for the clicked row (i.e. its DataContext
) and from there you can access the attributes in it.
<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>
Then you could do something like this in OptionsWindowCommandExecuted
. I might add that I'm not that familiar working with XmlDataProvider
so the Attributes["Status"].Value
might not be the recommended way :)
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";
}
}
}
Update
Some sample code to set different values depending on which RadioButton
the user checked
In 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;
}
}
}
精彩评论