WPF. We need a column filled with references.



  • Data source and DataGrid. The last column should be filled with references, the references will be one-time examples of somesite.ru/user?id=ID, where id comes from ItemSource. There's nothing I can do with Binding.

    <dxg:GridControl Name="grid" Grid.Row="1" FontFamily="Geometria" FontSize="10" >
            <dxg:GridControl.Columns>
                <dxg:GridColumn FieldName="Название" />
                <dxg:GridColumn FieldName="Издательство" />
                <dxg:GridColumn FieldName="Цена" />
                <dxg:GridColumn FieldName="Просмотры" />
                <dxg:GridColumn FieldName="Рейтинг" Width="100"/>
                <dxg:GridColumn>
                    <dxg:GridColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel HorizontalAlignment="Center">
                                <!--тут ссылка-->
                            </StackPanel>
                        </DataTemplate>
                    </dxg:GridColumn.CellTemplate>
                </dxg:GridColumn>
            </dxg:GridControl.Columns>
            <dxg:GridControl.View>
                <dxg:TableView NavigationStyle="Cell" FocusedRowChanged="TableView_FocusedRowChanged" AllowEditing="False">
    
            &lt;/dxg:TableView&gt;
        &lt;/dxg:GridControl.View&gt;
    &lt;/dxg:GridControl&gt;
    



  • What's complicated?

    Make an converter:

    class UriConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter,
                              CultureInfo culture)
        {
            var id = (string)value;
            return new Uri("http://somesite.ru/user?id=" + id);
        }
    
    public object ConvertBack(object value, Type targetType, object parameter,
                              CultureInfo culture)
    {
        throw new NotSupportedException();
    }
    

    }

    Then put it in your window's resources:

    <Window.Resources>
    <local:UriConverter x:Key="UriConverter"/>
    </Window.Resources>

    And use it to convert:

    <TextBlock>
    <Hyperlink
    NavigateUri="{Binding Mode=OneWay, Converter={StaticResource UriConverter}}"
    RequestNavigate="OnRequestNavigate"><Run Text="{Binding NavigateUri,
    RelativeSource={RelativeSource FindAncestor, AncestorType=Hyperlink}}"/></Hyperlink>
    </TextBlock>

    Subscription RequestNavigate It's necessary to make references.

    void OnRequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
    {
    var uri = e.Uri.AbsoluteUri;
    Task.Run(() => Process.Start(uri));
    e.Handled = true;
    }

    I think everything should work.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2