2016-07-23 47 views
1

有没有什么方法可以获得粘贴到ItemsControl的数据的索引,单击按钮时?获取来自ItemsControl的数据绑定索引

<ItemsControl x:Name="ic_schranke" DataContext="{Binding Schranke}" > 
       <ItemsControl.ItemTemplate> 
        <DataTemplate > 

         <Button Height="80" x:Name="btn_open" Click="btn_open_Click" HorizontalAlignment="Stretch" Style="{StaticResource TransparentStyle}"> 
          <Grid HorizontalAlignment="Stretch"> 
           <Grid.ColumnDefinitions> 
            <ColumnDefinition Width="1*"/> 
            <ColumnDefinition Width="2*"/> 
            <ColumnDefinition Width="1*"/> 
           </Grid.ColumnDefinitions> 
           <Grid.RowDefinitions> 
            <RowDefinition Height="1*"/> 
            <RowDefinition Height="30*"/> 
            <RowDefinition Height="1*"/> 
           </Grid.RowDefinitions> 

           <Rectangle x:Name="rect" Grid.ColumnSpan="3" Grid.Row="1" Opacity="0.65" Grid.Column="0" Fill="#FFCEEAFF"/> 

           <Border CornerRadius="25" Height="50" Width="50" HorizontalAlignment="Center" Grid.Column="0" Grid.Row="1"> 
            <Border.Background> 
             <ImageBrush ImageSource="/Assets/schranken.jpg" /> 
            </Border.Background> 
           </Border> 

           <TextBlock Name="schranken_name" Grid.Column="1" Grid.Row="1" Text="{Binding name}" VerticalAlignment="Center" HorizontalAlignment="Center" FontWeight="ExtraLight" Foreground="Black" /> 
          </Grid> 
         </Button> 

        </DataTemplate> 
       </ItemsControl.ItemTemplate> 
      </ItemsControl> 

这里是一个被绑定到IC样本数据:

List<Schranke> elements; 
     public UserPage() 
     { 
      this.InitializeComponent(); 

      elements = new List<Schranke>(); 
      for (var i = 1; i < 15; i++) 
       elements.Add(new Schranke() { name = $"Schranke NR:{i}" }); 


      this.ic_schranke.ItemsSource = elements; 


} 

这里是在按钮单击事件代码:

private async void btn_open_Click(object sender, RoutedEventArgs e) 
     { 
      Grid grid = (sender as Button).Content as Grid; 



      //Debug.WriteLine($"content {tb.Text} clicked"); 
      var tmp_background = grid.Background; 
      grid.Background = new SolidColorBrush(new Windows.UI.Color() { A = 255, R = 78, G = 170, B = 44 }); 
      VibrationDevice testVibrationDevice = VibrationDevice.GetDefault(); 
      testVibrationDevice.Vibrate(System.TimeSpan.FromMilliseconds(150)); 
      await Task.Delay(3000); 
      grid.Background = tmp_background; 
     } 
+0

为什么不使用命令而不是事件(更好的做法),并发送命令参数列表中的对象?或者至少是ID。 我不能给出一个示例代码,没有Schranke的定义 – MichaelThePotato

+0

Schranke是一个字符串名称只作为成员的类。 – student96

回答

1

也许沿东西线这个:

主持人 - 把这个放在数据上下文中

public class SchrankePresenter : INotifyPropertyChanged 
{ 
    private List<Schranke> _elements; 
    public List<Schranke> Elements 
    { 
     get { return _elements; } 
     set 
     { 
      _elements = value; 
      OnPropertyChanged("Elements"); 
     } 
    } 
    public ICommand ClickCommand { get; set; } 

    private void OnPropertyChanged(string propName) 
    { 
     PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName)); 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    public SchrankePresenter() 
    { 
     var elements = new List<Schranke>(); 
     for (var i = 1; i < 15; i++) 
      elements.Add(new Schranke() { Name = $"Schranke NR:{i}" }); 

     Elements = elements; 
     ClickCommand = new DelegateCommand(ClickAction); 
    } 

    public void ClickAction(Schranke item) 
    { 
     VibrationDevice.GetDefault().Vibrate(TimeSpan.FromMilliseconds(150)); 
    } 
} 

public class Schranke 
{ 
    public string Name { get; set; } 
} 

模板:

<ListView ItemsSource="{Binding Elements}"> 

      <ListView.ItemTemplate> 

       <DataTemplate> 

        <Button Height="80" 
          HorizontalAlignment="Stretch" 
          Command="{Binding ClickCommand}" 
          Style="{StaticResource TransparentStyle}"> 
         <Grid HorizontalAlignment="Stretch"> 
          <Grid.ColumnDefinitions> 
           <ColumnDefinition Width="1*" /> 
           <ColumnDefinition Width="2*" /> 
           <ColumnDefinition Width="1*" /> 
          </Grid.ColumnDefinitions> 
          <Grid.RowDefinitions> 
           <RowDefinition Height="1*" /> 
           <RowDefinition Height="30*" /> 
           <RowDefinition Height="1*" /> 
          </Grid.RowDefinitions> 

          <Rectangle x:Name="rect" 
             Grid.Row="1" 
             Grid.Column="0" 
             Grid.ColumnSpan="3" 
             Fill="#FFCEEAFF" 
             Opacity="0.65" /> 

          <Border Grid.Row="1" 
            Grid.Column="0" 
            Width="50" 
            Height="50" 
            HorizontalAlignment="Center" 
            CornerRadius="25"> 
           <Border.Background> 
            <ImageBrush ImageSource="/Assets/schranken.jpg" /> 
           </Border.Background> 
          </Border> 

          <TextBlock Name="schranken_name" 
             Grid.Row="1" 
             Grid.Column="1" 
             HorizontalAlignment="Center" 
             VerticalAlignment="Center" 
             FontWeight="ExtraLight" 
             Foreground="Black" 
             Text="{Binding Name}" /> 
         </Grid> 
        </Button> 

       </DataTemplate> 

      </ListView.ItemTemplate> 

     </ListView> 

您可以使用情节提要/的visualSTATE做背景动画

编辑:关于DelegateCommand,这是一个简单的实现我用我的WPF应用程序 -

internal class DelegateCommand<T> : ICommand 
{ 
    private Action<T> _clickAction; 

    public DelegateCommand(Action<T> clickAction) 
    { 
     _clickAction = clickAction; 
    } 

    public event EventHandler CanExecuteChanged; 

    public bool CanExecute(object parameter) 
    { 
     return true; 
    } 

    public void Execute(object parameter) 
    { 
     _clickAction((T)parameter); 
    } 
} 
+0

我认为我的想法解决了我的问题,但我没有得到与DelegateCommand(ClickAction)相关的建议。你能解释这部分吗? – student96

+0

我在原始响应中添加了一个解释,它是ICommand的一个快速而基本的实现,可以使用任何符合您需要的东西。这通常适合我的。 – MichaelThePotato

+0

将类添加到项目后,我得到此错误代码“使用泛型类型'DelegateCommand '需要'1'类型参数”。我有一种感觉,你知道这个问题,并知道解决这个问题。 – student96

相关问题