这是我的xaml代码。
<sdk:DataGrid x:Name="dgMarks" CanUserResizeColumns="False" SelectionMode="Single" AutoGenerateColumns="False" VerticalAlignment="Top" IsReadOnly="True" Margin="13,44,0,0" RowDetailsVisibilityChanged="dgMarks_RowDetailsVisibilityChanged" RowDetailsVisibilityMode="Collapsed" Height="391" HorizontalAlignment="Left" Width="965" SelectionChanged="dgMarks_SelectionChanged" VerticalScrollBarVisibility="Visible" >
<sdk:DataGrid.Columns>
<sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button x:Name="myButton"
Click="ExpandMarks_Click">
<TextBlock Text="{Binding Level}" TextWrapping="NoWrap" ></TextBlock>
<Image x:Name="imgMarks" Stretch="None"/>
</Button>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn Header="Name" Visibility="Collapsed">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate >
<sdk:Label Content="{Binding Name}"/>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
<sdk:DataGridTemplateColumn Header="Marks" Width="80">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<sdk:Label Content="{Binding Marks}"/>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
从数据库我得到这些值
name marks Level
abc 23 0
xyz 67 1
yu 56 0
aa 89 1
这里我绑定了datagrid的这些值。 我有一个棘手的事情要做。基于我应该绑定图像的水平 如果级别值为1则绑定图像。如果级别值为0,则不绑定该行的图像
我知道这是我们需要处理的方式,但我应该在哪里写下这些事件的代码?
Image imgLevel = (Image)templateTrendScore.FindName("imgMarks");
if (level1==1)
{
imgLevel .Source = new BitmapImage(new Uri("/Images/image1.JPG", UriKind.Relative));
}
提前感谢任何帮助
答案 0 :(得分:2)
值转换器是你追求的项目,假设你有超过0和1的等级(否则你已经使用了布尔值?)
这是值转换器: -
[ContentProperty("Items")]
public class IndexToObjectConverter : IValueConverter
{
private readonly ObservableCollection<object> myCol = new ObservableCollection<object>();
public ObservableCollection<object> Items { get { return myCol; } }
#region IValueConverter Members
public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return Items[(int)value];
}
public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException("This converter only works for one way binding");
}
#endregion
}
在usercontrol资源中,您将拥有: -
<UserControl.Resources>
<local:IndexToObjectConverter x:Key="LevelToImage">
<BitmapImage />
<BitmapImage UriSource="Test.png" />
</local:IndexToObjectConverter>
</UserControl.Resources>
现在可以绑定Image控件的Source
属性,如下所示: -
<Image Source="{Binding Level, Converter={StaticResource LevelToImage}}" />
现在,当您发明新的级别编号时,您可以向转换器添加元素。
顺便说一句,您似乎在TextBlock
内同时拥有Image
和Button
。 Button
只能容纳一个对象,因此您需要将这两个对象包含在某些面板中,例如Grid
。
答案 1 :(得分:0)
将图像放在xaml中,然后使用IValueConverter将其Visibility
属性绑定到Level:
public class LevelToVisibilityConverter : IValueConverter
{
/// <exception cref="ArgumentException">TargetType must be Visibility</exception>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if(!(value is int))
throw new ArgumentException("Source must be of type int");
if(targetType != typeof(Visibility))
throw new ArgumentException("TargetType must be Visibility");
int v = (int) value;
if (v == 1)
return Visibility.Visible;
else
return Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}