There is a data source and DataGrid. It is necessary to fill the last column with links, the links will be of the same type of example somesite.ru/user?id=ID, where id comes from the ItemSource. I can't figure it out 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"> </dxg:TableView> </dxg:GridControl.View> </dxg:GridControl> 

    2 answers 2

    What's so complicated?

    Create a 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 the resources of your window:

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

    And use for converting:

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

    A subscription to RequestNavigate needed for links to work.

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

    Everything seems to be working.

    • So it turns out that the UriConverter gets a value from the database? Do not tell a good source of information on Binding. - Ilya
    • @Ilya, the value of the current row of the current column gets into the converter there. Where it comes from in your case is incomprehensible. But, apparently, from the base :) - iRumba
    • What comes after the Binding enters the UriConverter . In your case, id. As for what to read, I would advise books from here . For example, WPF Unleashed, Adam Nathan. - VladD

    And even easier through the formatting

    here's what the binding looks like

     {Binding YourProperty, StringFormat='somesite.ru/user?id={0}'} 

    Well, in your case, just

     {Binding StringFormat='somesite.ru/user?id={0}'} 

    In short, everything is the same as VladD wrote, but without a converter

    • And if for example in this cell to write values ​​from other cells of the same row - Ilya
    • @ Ilya For this there is a MultiBinding. But in your case, I'm not sure how best to do it. You use DevExpress controls, but I don’t really know them :) The general sense is that the cell values ​​should not be entered via FieldName, but through TextBlock + binding, then you can take all the values ​​through multibinding and also insert them into the link via StringFormat - iRumba
    • @iRumba: This works if the Binding goes to a string field. But with the Uri type NavigateUri it won't work . I also tried first :) - VladD
    • @VladD, so there on any string. The same table and data are most likely pulled from DataTable - iRumba
    • @iRumba: It seems like Hyperlink.RequestUri not a string. The question is not in the data source, but in where they are being sent. Try it! - VladD