2015-06-12 36 views
6

Tôi có .xaml sau cho DataGrid và muốn hiển thị ẩn nút/với điều kiện nhất định trong cs mã .xaml như sauLàm thế nào để truy cập vào nút từ DataGridTemplateColumn.CellTemplate

<DataGridTemplateColumn Header="Action" Width="auto" > 
    <DataGridTemplateColumn.CellTemplate> 
     <DataTemplate> 
      <StackPanel Orientation="Horizontal"> 
       <Button x:Name="btnConfirm" Content="Confirm" Click="ConfirmButton_Click" Height="auto" Width="auto" Opacity="100" Background="Transparent" BorderBrush="Transparent" HorizontalAlignment="Left"/> 
       <Button x:Name="btnDecline" Content="Decline" Click="btnDecline_Click" Height="auto" Width="auto" Opacity="100" Background="Transparent" BorderBrush="Transparent" HorizontalAlignment="Left" /> 
       <Button x:Name="btnCancel" Content="Cancel" Click="btnCancel_Click" Height="auto" Width="auto" Opacity="100" Background="Transparent" BorderBrush="Transparent" HorizontalAlignment="Left"/> 
      </StackPanel> 
     </DataTemplate> 
    </DataGridTemplateColumn.CellTemplate> 
</DataGridTemplateColumn> 

và cs đang là như

foreach (sp_ProcessingJobsResult item in grdUnConfirmJobs.ItemsSource) 
{ 
var row = grdUnConfirmJobs.ItemContainerGenerator.ContainerFromItem(item) as System.Windows.Controls.DataGridRow; 
if (item.Status == "Cancellation Requested.") 
    { 
     //how find control  
    } 
} 
+0

Tôi đã chỉnh sửa tiêu đề của bạn. Vui lòng xem, "[Câu hỏi có nên bao gồm" thẻ "trong tiêu đề của họ không?] (Http://meta.stackexchange.com/questions/19190/)", trong đó sự đồng thuận là "không, họ không nên". –

+0

bản sao có thể có của http://stackoverflow.com/questions/19379946/how-to-access-a-control-within-data-template-from-code-behind – Ahmad

Trả lời

0

Bạn phải sử dụng Binding cho việc này. Vì vậy, chúng ta hãy tiếp cận nhanh:

Đây là mã đằng sau cho việc này:

public partial class MainWindow : Window 
{ 
    public bool ButtonIsVisible 
    { 
     get { return (bool)GetValue(ButtonIsVisibleProperty); } 
     set { SetValue(ButtonIsVisibleProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for ButtonIsVisible. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty ButtonIsVisibleProperty = 
     DependencyProperty.Register("ButtonIsVisible", typeof(bool), typeof(MainWindow), new UIPropertyMetadata(true)); 

    public ObservableCollection<Person> items { get; set; } 

    public MainWindow() 
    { 
     InitializeComponent(); 
     items = new ObservableCollection<Person>(); 
     items.Add(new Person() { Name = "FirstName" }); 
     items.Add(new Person() { Name = "SecondName" }); 

     this.DataContext = this; 
    } 
} 

Đây là mô hình cho ví dụ của tôi:

public class Person : INotifyPropertyChanged 
{ 
    private string _Name; 

    public string Name 
    { 
     get { return _Name; } 
     set 
     { 
      _Name = value; 
      PropertyChanged(this, new PropertyChangedEventArgs("Name")); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged = delegate { }; 
} 

Visibility của bạn không phải là một kiểu bool để chúng ta cần một chuyển đổi cho việc này:

public class BoolToVis : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     var isVisible = (bool)value; 

     return isVisible ? Visibility.Visible : Visibility.Hidden; 
    } 

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

Và đây là toàn bộ mã XAML:

<Window x:Class="DataGridCellsBackground.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525" 
    xmlns:local="clr-namespace:DataGridCellsBackground"  > 
<Grid> 
    <Grid.Resources> 
     <local:BoolToVis x:Key="BoolTovisibilityConverter"/> 
    </Grid.Resources> 
    <DataGrid SelectionUnit="Cell" 
       ItemsSource="{Binding items}" 
       AutoGenerateColumns="False"> 
     <DataGrid.Resources> 
     </DataGrid.Resources> 
     <DataGrid.Columns> 
      <DataGridTemplateColumn> 
       <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <TextBlock Text="{Binding Name}"/> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellTemplate> 
      </DataGridTemplateColumn> 
      <DataGridTemplateColumn> 
       <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <Button Content="Visible" Visibility="{Binding RelativeSource={RelativeSource AncestorType=DataGrid}, Path=DataContext.ButtonIsVisible, Converter={StaticResource BoolTovisibilityConverter}}"/> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellTemplate> 
      </DataGridTemplateColumn> 
     </DataGrid.Columns> 
    </DataGrid> 
</Grid> 

Trên thực tế bạn thấy một cú pháp Binding lạ tại Buttons Tầm nhìn. Tại sao vậy? Tôi giả sử bạn không cần chức năng này trong mô hình vì vậy tôi đã trở lại DataContext của DataGrid để đạt được DependencyProperty đó. Tôi không hoàn toàn chắc chắn về kịch bản của bạn nhưng tôi đã cho bạn thấy một số cơ chế để dễ dàng sử dụng.

Các vấn đề liên quan