0

WPF で Datagrid コントロールを作成しました。のみボタンを表示する方法

ボタンがボタン-列-セルに表示されるように、それを選択した行。

XAML:
<DataGridTemplateColumn x:Name="Button-Column" Header="H." Width="50">
  <DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
      <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding Path=ObjectType}" Value="E">
           <Setter Property="Visibility" Value="Hidden" />
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=ObjectType}" Value="F">
           <Setter Property="Visibility" Value="Hidden" />
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=ObjectType}" Value= "P">
            <Setter Property="Visibility" Value="Hidden" />
        </DataTrigger>
      </DataTemplate.Triggers>              
      <Button Name="btnTable" Visibility="{Binding Path=ObjectType}" Height="20"  
              Width="25" Click="Button_Table_Click">
      </Button>
     </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
  </DataGridTemplateColumn>
4

1 に答える 1

0

multibinding を使用したソリューションは次のとおりです。

xaml:

   <Window x:Class="WpfApplication8.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:WpfApplication8="clr-namespace:WpfApplication8"
            Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <WpfApplication8:ButtonVisibilityConverter x:Key="buttonVisibilityConverter"/>
        </Window.Resources>
        <DataGrid x:Name="dataGrid" SelectedItem="{Binding MySelectedItem}" ItemsSource="{Binding MyObjectList}" SelectionMode="Single">
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="H." Width="50">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button 
                                    Height="20"  
                                    Width="25">
                                <Button.Visibility>
                                    <MultiBinding Converter="{StaticResource buttonVisibilityConverter}">
                                        <Binding Path="Parent.MySelectedItem"/>
                                        <Binding></Binding>
                                    </MultiBinding>
                                </Button.Visibility>
                            </Button>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Window>

コードビハインド:

namespace WpfApplication8
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        public class Toto
        {
            public object Parent { get; set; }

            public string Name { get; set; }
            public string Content { get; set; }

            public Toto(string name, string content, object parent)
            {
                this.Name = name; this.Content = content; this.Parent = parent;
            }
        }

        private object _mySelectedItem;

        public object MySelectedItem
        {
            get { return _mySelectedItem; }
            set
            {
                _mySelectedItem = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs("MySelectedItem"));
                }
            }
        }

        private List<Toto> _myObjectList;

        public List<Toto> MyObjectList
        {
            get { return _myObjectList; }
            set { _myObjectList = value; }
        }

        public MainWindow()
        {
            this.MyObjectList = new List<Toto>
            {
                new Toto("toto1", "content toto1", this),
                new Toto("toto2", "content toto2", this)
            };

            InitializeComponent();

            this.DataContext = this;
        }
    }
}

コンバーター:

class ButtonVisibilityConverter : IMultiValueConverter
{
    #region IMultiValueConverter Members

    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (values[0] == values[1])
        {
            return Visibility.Visible;
        }
        else
        {
            return Visibility.Hidden;
        }
    }

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

    #endregion
}
于 2012-08-16T11:41:02.363 に答える