c#xaml绑定图像路径名动态 [英] c# xaml binding image pathname dynamic

查看:86
本文介绍了c#xaml绑定图像路径名动态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!



我在我的数据网格中有这个数据模板



 <   DataGridTemplateColumn      标题  =  Ferdig   宽度  =  * >  
< ; DataGridTemplateColumn.CellTemplate >
< DataTemplate >
< 网格 高度 = auto 背景 = #FF3F3F46 >
< 网格 高度 = 自动 >
< Grid.RowDefinitions >
< RowDefinition 高度 = 自动 / >
< RowDefinition 高度 = 自动 / >
< / Grid.RowDefinitions >
< TextBox 文字 = {Binding Tegn} IsReadOnly = True Grid.Row = 0 保证金 = 5,5,0,5 FontSize = 30 / >
< TextBox 文字 = {Binding Varenr} IsReadOnly = True Grid.Row = 0 保证金 = 0,5,5,5 FontSize = 18 Horizo​​ntalAlignment = 钻机ht / >
< 网格 高度 = 自动 Grid.Row = 1 >
< Grid.ColumnDefinitions < span class =code-keyword>>
< ColumnDefinition 宽度 = * / >
< ColumnDefinition < span class =code-attribute>宽度 = * / >
< / Grid.ColumnDefinitions >
< TextBlock 文字 = Utfordring Grid.Column = 0 FontSize = 12 保证金 = 5,5,5,5 / >
< 图像 来源 = P:\ _Avd_Storkunde \ 02 Dosepakking(ML)\ 20 IT(JSH)\ 2019 Tablettbilder\2091 Dagens bilder\5.jpg Grid.Column = 0 保证金 = 5,25,5,5 Horizo​​ntalAlignment = 拉伸 VerticalAlignment = Top / >
< < span class =code-leadattribute> TextBlock Text = Forslag til ti ltak Grid.Column = 1 FontSize = 12 保证金 = 5 ,5,5,5 / >
< TextBox 文本 = {Binding Forslag_til_tiltak} IsReadOnly = True Grid.Column = 1 保证金 = 5,25,5,5 VerticalAlignment < span class =code-keyword> = Top FontSize = 16 TextWrapping = Wrap / >
< / Grid >
< / Grid >
< / Grid >
< / DataTemplate >
< / DataGridTemplateColumn.CellTemplate >







如何使图像源动态和绑定?



我想这样做:



 <  图像   来源  =  P:\ _Avd_Storkunde \ 02 Dosepakking(ML)\ 20 IT(JSH)\\ \\ _209 Tablettbilder \ 2091 Dagens bilder \        {Binding     Varenr}     +    。jpg    Grid.Column   =  0     保证金  =  5,25,5,5    Horizo​​ntalAlignment   = 拉伸    VerticalAlignment   = 顶部  /  >  







我如何达到我的目标?

解决方案

有几种方法可以实现这个目标:



1.)在您的数据绑定对象中提供一个属性,该属性包含一个Uri,您可以轻松绑定到



2.)使用Binding的 StringFormat-property [ ^ ]



3.)使用自定义值转换器 [ ^ ]:



 命名空间 WpfApplication1 {
[ValueConversion( typeof (< span class =code-keyword> object ), typeof String ))]
public class StringFormatConverter:IValueConverter {
public object 转换( object value ,输入targetType, object 参数,CultureInfo文化){
var formatString = string .IsNullOrEmpty(formatString的)? {0}:FormatString;
return string .Format(culture,FormatString,);
}

public object ConvertBack( object value ,输入targetType, object 参数,CultureInfo culture){
throw new NotImplementedException();
}

public string FormatString {获得; set ; }
}
}

}





在xaml中定义一个实例:

 <   Window     xmlns:local   =  clr-namespace:WpfApplication1 >  
< Window.Ressources >
< local:StringFormatConverter x:key = imageSourceConverter FormatString = p:\ .Folder\subfolder \ {0} .jpg / >
< / Window .Ressources >

<! - < span class =code-keyword> 使用转换器 - >
< 图片 来源 = {Binding Varenr,Converter = {StaticResource imageSourceConverter}} / >


Hello!

I have this datatemplate in my datagrid

<DataGridTemplateColumn  Header="Ferdig" Width="*">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate >
                                <Grid Height="auto" Background="#FF3F3F46">
                                    <Grid Height="Auto">
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="Auto"/>
                                            <RowDefinition Height="Auto"/>
                                        </Grid.RowDefinitions>
                                        <TextBox  Text="{Binding Tegn}" IsReadOnly="True" Grid.Row="0" Margin="5,5,0,5" FontSize="30"/>
                                        <TextBox  Text="{Binding Varenr}" IsReadOnly="True" Grid.Row="0" Margin="0,5,5,5" FontSize="18" HorizontalAlignment="Right"/>
                                        <Grid Height="Auto" Grid.Row="1">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="*"/>
                                                <ColumnDefinition Width="*"/>
                                            </Grid.ColumnDefinitions>
                                            <TextBlock Text="Utfordring" Grid.Column="0" FontSize="12"  Margin="5,5,5,5"/>
                                            <Image Source="P:\Avd_Storkunde\02 Dosepakking (ML)\20 IT (JSH)\209 Tablettbilder\2091 Dagens bilder\5.jpg" Grid.Column="0"  Margin="5,25,5,5" HorizontalAlignment="Stretch" VerticalAlignment="Top"/>
                                            <TextBlock Text="Forslag til tiltak" Grid.Column="1" FontSize="12"  Margin="5,5,5,5"/>
                                            <TextBox Text="{Binding Forslag_til_tiltak}" IsReadOnly="True" Grid.Column="1"  Margin="5,25,5,5" VerticalAlignment="Top" FontSize="16" TextWrapping="Wrap"/>
                                        </Grid>
                                    </Grid>
                                </Grid>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>




How can i make the imagesource dynamic and bound?

I want to do something like this:

<Image Source="P:\Avd_Storkunde\02 Dosepakking (ML)\20 IT (JSH)\209 Tablettbilder\2091 Dagens bilder\" + {Binding Varenr} + ".jpg" Grid.Column="0"  Margin="5,25,5,5" HorizontalAlignment="Stretch" VerticalAlignment="Top"/>




How can i arrive at my goal?

解决方案

there are several ways to achieve this:

1.) provide a property in your databound object that holds an Uri you can easyly bind to

2.) use the Binding''s StringFormat-property[^]

3.) use a custom value converter[^]:

namespace WpfApplication1 {
  [ValueConversion(typeof(object), typeof(String))]
  public class StringFormatConverter : IValueConverter {
      public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
          var formatString = string.IsNullOrEmpty(FormatString)? "{0}" : FormatString;
          return string.Format(culture, FormatString, value);
      }

      public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
          throw new NotImplementedException();
      }

      public string FormatString{ get; set; }
  }
}

}



define an instance inside your xaml:

<Window xmlns:local="clr-namespace:WpfApplication1">
  <Window.Ressources>
     <local:StringFormatConverter x:key="imageSourceConverter" FormatString="p:\Folder\subfolder\{0}.jpg" /> 
  </Window.Ressources>

<!--use the converter -->
<Image Source="{Binding Varenr, Converter={StaticResource imageSourceConverter}}" />


这篇关于c#xaml绑定图像路径名动态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆