将DataTemplate按钮IsEnabled绑定到itemssource元素

时间:2015-02-26 01:19:24

标签: wpf button binding datatemplate isenabled

我有以下情况,(用实际代码更新我的表示)

数据类为,

public class X : INotifyPropertyChanged
    {
        public decimal patientID { get; set; }
        public decimal medicationID { get; set; }
        public decimal documentID { get; set; }
        public decimal prescriptionID { get; set; }
        public string prescriptionOrderNumber { get; set; }
        public string patientName { get; set; }
        public string medicationName { get; set; }
        public string medicationQuantity { get; set; }
        public string medicationInstructions { get; set; }
        public string authorizingProvider { get; set; }
        public string stateName  { get; set; }
        public bool prescriptionPrinted {get; set; }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
    }
}

VM class has an observable collection property as,

 private ObservableCollection<X> _items = new ObservableCollection<X>();

        public ObservableCollection<X> items 
        { 
            get { return _items; }
        }


view
----
   <UserControl.DataContext>
        <viewModel:VM/>
    </UserControl.DataContext>

    <ListView Grid.Row="1" Margin="10" Name="epcslist" SelectionMode="Single" ItemsSource="{Binding items}"  >
            <ListView.View>
                 <GridView>
                    <GridViewColumn  Width="30">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Button Name="btnPrintPrescription"  Content=" P "  ToolTip="Print prescription" 
                                        IsEnabled="{Binding Path=prescriptionPrinted}"
                                        Command="{Binding DataContext.PrintPrescriptionCommand, RelativeSource={RelativeSource AncestorType=ListView}}"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>

                    <GridViewColumn  Width="30">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Button Name="btnOpenPatientChart" Content=" O " ToolTip="Open patient's chart"
                                        Command="{Binding DataContext.OpenPatientChartCommand, RelativeSource={RelativeSource AncestorType=ListView}}">
                                </Button>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Width="120" DisplayMemberBinding="{Binding patientName}" >
                        <GridViewColumn.Header>
                            <GridViewColumnHeader Tag="Patient" Click="EpcslistColumnHeader_Click">Patient</GridViewColumnHeader>
                        </GridViewColumn.Header>
                    </GridViewColumn>
                    <GridViewColumn Width="150" DisplayMemberBinding="{Binding medicationName}" >
                        <GridViewColumn.Header>
                            <GridViewColumnHeader Tag="Medication" Click="EpcslistColumnHeader_Click">Medication</GridViewColumnHeader>
                        </GridViewColumn.Header>
                    </GridViewColumn>
                    <GridViewColumn Width="50" DisplayMemberBinding="{Binding medicationQuantity}" >
                        <GridViewColumn.Header>
                            <GridViewColumnHeader Tag="Qty" Click="EpcslistColumnHeader_Click">Qty</GridViewColumnHeader>
                        </GridViewColumn.Header>
                    </GridViewColumn>
                    <GridViewColumn Width="120" DisplayMemberBinding="{Binding medicationInstructions}" >
                        <GridViewColumn.Header>
                            <GridViewColumnHeader Tag="Instructions" Click="EpcslistColumnHeader_Click">Instructions</GridViewColumnHeader>
                        </GridViewColumn.Header>
                    </GridViewColumn>
                    <GridViewColumn Width="120" DisplayMemberBinding="{Binding authorizingProvider}" >
                        <GridViewColumn.Header>
                            <GridViewColumnHeader Tag="Provider" Click="EpcslistColumnHeader_Click">Authorizing Provider</GridViewColumnHeader>
                        </GridViewColumn.Header>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>

我正在尝试将按钮的IsEnabled绑定到处理的打印源但它不起作用。我需要帮助来绑定它,以便在加载listview时根据bool值我需要启用或禁用按钮,同时将项目加载到列表视图中。

非常感谢任何帮助。

谢谢, UB。

1 个答案:

答案 0 :(得分:0)

首先,Binded属性必须是Property而不是Field所以你应该把你的代码放在这个 的 XAML

<ListView ItemsSource="{Binding}">
    <ListView.View>
        <GridView>
            <GridViewColumn >
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <Button Content="P" IsEnabled="{Binding Element3 }"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn DisplayMemberBinding="{Binding Element1}" />
            <GridViewColumn DisplayMemberBinding="{Binding Element2}" />
        </GridView>
    </ListView.View>
</ListView>

<强> CS

public class Source
{
    public string Element1
    { get; set; }
    public string Element2
    { get; set; }
    public bool Element3
    { get; set; }
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
    List<Source> Items = new List<Source>();
    Items.Add(new Source() { Element1 = "Name11", Element2 = "Name12", Element3 = false });
    Items.Add(new Source() { Element1 = "Name21", Element2 = "Name22", Element3 = true });
    Items.Add(new Source() { Element1 = "Name31", Element2 = "Name32", Element3 = true });
    Items.Add(new Source() { Element1 = "Name41", Element2 = "Name42", Element3 = false });

    this.DataContext = Items;
}