2010-05-19 51 views
3
<UserControl x:Class="SLGridImage.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" 
    d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"> 

    <UserControl.Resources> 
     <local:LevelToVisibilityConverter x:Key="LevelToVisibility" /> 
    </UserControl.Resources> 


    <Grid x:Name="LayoutRoot" Background="White"> 
     <sdk:DataGrid x:Name="dgMarks" CanUserResizeColumns="False" SelectionMode="Single" 
       AutoGenerateColumns="False" 
         VerticalAlignment="Top" 
         ItemsSource="{Binding MarkCollection}" 
         IsReadOnly="True" 
         Margin="13,44,0,0" 
         RowDetailsVisibilityMode="Collapsed" Height="391" 
         HorizontalAlignment="Left" Width="965" 
         VerticalScrollBarVisibility="Visible" > 
      <sdk:DataGrid.Columns> 
       <sdk:DataGridTemplateColumn> 
        <sdk:DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
          <Button x:Name="myButton" 
          Click="myButton_Click"> 
           <StackPanel Orientation="Horizontal"> 
            <Image Margin="2, 2, 2, 2" x:Name="imgMarks" Stretch="Fill" Width="12" Height="12" 
              Source="Images/test.png" 
              VerticalAlignment="Center" 
              HorizontalAlignment="Center" 
             Visibility="{Binding Level, Converter={StaticResource LevelToVisibility}}" 
            /> 
            <TextBlock Text="{Binding Level}" TextWrapping="NoWrap" ></TextBlock> 

           </StackPanel> 
          </Button> 
         </DataTemplate> 
        </sdk:DataGridTemplateColumn.CellTemplate> 
       </sdk:DataGridTemplateColumn> 
       <sdk:DataGridTemplateColumn Header="Name" > 
        <sdk:DataGridTemplateColumn.CellTemplate> 
         <DataTemplate > 
          <Border> 
           <TextBlock Text="{Binding Name}" /> 
          </Border> 
         </DataTemplate> 
        </sdk:DataGridTemplateColumn.CellTemplate> 
       </sdk:DataGridTemplateColumn> 

       <sdk:DataGridTemplateColumn Header="Marks" Width="80"> 
        <sdk:DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
          <Border> 
           <TextBlock Text="{Binding Marks}" /> 
          </Border> 
         </DataTemplate> 
        </sdk:DataGridTemplateColumn.CellTemplate> 
       </sdk:DataGridTemplateColumn> 
      </sdk:DataGrid.Columns> 
     </sdk:DataGrid> 
    </Grid> 
</UserControl> 

中的.cs:LevelToVisibilityConverter在Silverlight 4

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 
using System.Collections.ObjectModel; 
using System.ComponentModel; 

namespace SLGridImage 
{ 
    public partial class MainPage : UserControl 
    { 
     private MarksViewModel model = new MarksViewModel(); 
     public MainPage() 
     { 
      InitializeComponent(); 
      this.DataContext = model; 
     } 

     private void myButton_Click(object sender, RoutedEventArgs e) 
     { 

     } 
    } 

    public class MarksViewModel : INotifyPropertyChanged 
    { 

     public MarksViewModel() 
     { 
      markCollection.Add(new Mark() { Name = "ABC", Marks = 23, Level = 0 }); 
      markCollection.Add(new Mark() { Name = "XYZ", Marks = 67, Level = 1 }); 
      markCollection.Add(new Mark() { Name = "YU", Marks = 56, Level = 0 }); 
      markCollection.Add(new Mark() { Name = "AAA", Marks = 89, Level = 1 }); 
     } 


     private ObservableCollection<Mark> markCollection = new ObservableCollection<Mark>(); 
     public ObservableCollection<Mark> MarkCollection 
     { 
      get { return this.markCollection; } 
      set 
      { 
       this.markCollection = value; 
       OnPropertyChanged("MarkCollection"); 
      } 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 

     public void OnPropertyChanged(string propName) 
     { 
      if (PropertyChanged != null) 
       this.PropertyChanged(this, new PropertyChangedEventArgs(propName)); 
     } 
    } 

    public class Mark 
    { 
     public string Name { get; set; } 
     public int Marks { get; set; } 
     public int Level { get; set; } 
    } 

    public class LevelToVisibilityConverter : System.Windows.Data.IValueConverter 
    { 
     #region IValueConverter Members 

     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      Visibility isVisible = Visibility.Collapsed; 
      if ((value == null)) 
       return isVisible; 
      int condition = (int)value; 
      isVisible = condition == 1 ? Visibility.Visible : Visibility.Collapsed; 
      return isVisible; 
     } 

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

     #endregion 
    } 
} 

當我運行在這條線

<UserControl.Resources> 
     <local:LevelToVisibilityConverter x:Key="LevelToVisibility" /> 
    </UserControl.Resources> 

類型收到錯誤 '地方:LevelToVisibilityConverter' 沒有被發現。驗證您是否缺少程序集引用,並且所有引用的程序集都已構建。

我在這裏錯過了什麼? 期待解決方案,謝謝。

回答

1

您需要在Assembly中包含LevelToVisibilityConverter所在的Namespace的Xml-名稱空間。

假設你LevelToVisibilityConverter在命名空間SLGridImage你必須添加XML命名空間(XMLNS:地方= 「CLR的命名空間:SLGridImage」)如下

<UserControl x:Class="SLGridImage.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:local="clr-namespace:SLGridImage" 
    <!-- Rest of the declaration --> 
> 
</UserControl> 
+0

嗯......你的答案現在比你第一次發佈它的時候要精確得多。 – slugster 2010-05-19 09:14:43

+0

@slugster:是的,在我再次閱讀問題後,我更新了答案。 – Jehof 2010-05-19 10:32:08

1

補充一點:

xmlns:local="clr-namespace:SLGridImage" 

到您的XAML的開始。使用intellisense只是爲了防止出現小的錯字:)問題是您沒有定義引用的命名空間/程序集local