资源样式在DataTemplate中不起作用

时间:2016-09-29 16:15:26

标签: wpf textbox styles datatemplate

我有以下资源:

<UserControl.Resources>
    <Style TargetType="{x:Type TextBox}" >
        <Setter Property="Background" Value="#FFFFBF" />
        <Setter Property="IsEnabled" Value="{Binding Path=EntryEnabled, Mode=TwoWay}" />
    </Style>

    <DataTemplate x:Key="CommodityTemplate">
        <Grid Width="218" HorizontalAlignment="Right">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="40"/>
                <ColumnDefinition Width="30"/>
                <ColumnDefinition Width="30"/>
            </Grid.ColumnDefinitions>
            <Border Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="4" BorderBrush="Black" BorderThickness="0,0,0,.5" Background="LightCyan" />
            <TextBlock Grid.Column="0" Text="{Binding Path=Item_Name}" HorizontalAlignment="Stretch" VerticalAlignment="Center" FontSize="14" />
            <StackPanel Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Center" Orientation="Vertical" Margin="2,0" >
                <TextBlock Text="{Binding Path=Size}" HorizontalAlignment="Right" FontSize="8" />
                <TextBlock Text="{Binding Path=Unit}" HorizontalAlignment="Right" FontSize="8" />
            </StackPanel>
            <TextBox Grid.Column="2" Text="{Binding Path=Qty_Order, Mode=TwoWay}" FontSize="12" Margin="2,1"  />
            <TextBox Grid.Column="3" Text="{Binding Path=Qty_Filled, Mode=TwoWay}" FontSize="12" Margin="2,1" IsEnabled="{Binding Path=EntryEnabled, Mode=TwoWay}" />
        </Grid>
    </DataTemplate>

    <DataTemplate x:Key="CategoryTemplate">
        <Grid Width="225">
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition Width="30"/>
                <ColumnDefinition Width="30"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="40" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Border Grid.Row="0" Grid.Column="0" Grid.RowSpan="2" Grid.ColumnSpan="3" BorderBrush="Black" BorderThickness=".5" Background="AliceBlue"/>
            <Border Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3" BorderBrush="Black" BorderThickness="0,0,0,.5" />
            <Border Grid.Row="0" Grid.Column="1" BorderBrush="Black" BorderThickness=".5,0" />
            <TextBlock Grid.Column="0" Text="{Binding Path=Category.Description}" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="14" FontWeight="Bold" TextWrapping="Wrap" Margin="5,0"/>
            <TextBlock Grid.Column="1" Text="Quantity Ordered" FontSize="7" TextWrapping="Wrap" HorizontalAlignment="Center" VerticalAlignment="Center" >
                <TextBlock.LayoutTransform>
                    <RotateTransform Angle="-90"/>
                </TextBlock.LayoutTransform>
            </TextBlock>
            <TextBlock Grid.Column="2" Text="Quantity filled" FontSize="7" TextWrapping="Wrap" HorizontalAlignment="Center" VerticalAlignment="Center" >
                <TextBlock.LayoutTransform>
                    <RotateTransform Angle="-90"/>
                </TextBlock.LayoutTransform>
            </TextBlock>
            <ListView Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3" ItemsSource="{Binding Path=Items, Mode=TwoWay}" ItemTemplate="{StaticResource CommodityTemplate}" />
        </Grid>
    </DataTemplate>

</UserControl.Resources>

在我的控件中,我有很多TextBox控件。

然后我有以下LiewView控件,它使用CategoryTemplate。此模板包含另一个使用CommodityTemplate的ListView控件。

<ListView Grid.Row="1" Grid.Column="1" ItemsSource="{Binding Path=Category_List}" ItemTemplate="{StaticResource CategoryTemplate}" 
          ScrollViewer.VerticalScrollBarVisibility="Disabled" Background="LightGray" >
    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel Orientation="Vertical" />
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>
</ListView>

我还有一个CheckBox控件,如果选中,我希望禁用所有TextBox控件。

<CheckBox Grid.Row="3" Grid.Column="1" Content="Has Been Picked-Up" FlowDirection="RightToLeft" IsChecked="{Binding Path=Received, Mode=TwoWay}" 
          HorizontalAlignment="Right" VerticalAlignment="Center" Margin="20,0" Padding="5,0" IsEnabled="{Binding Path=CBrec_Enabled, Mode=TwoWay}" />

在我的ViewModel中可以找到Received和EntryEnabled属性。

    #region Received

    /// <summary>ViewModel property: Received of type: Boolean points to OrderForm.Received</summary>
    public Boolean Received
    {
        get
        {
            if (OrderForm == null) return false;
            return OrderForm.Received;
        }
        set
        {
            if (OrderForm == null) return;
            OrderForm.Received = value;
            OnPropertyChanged("Received");
            OnPropertyChanged("EntryEnabled");
        }
    }

    #endregion

   #region EntryEnabled

    /// <summary>Property EntryEnabled of type Boolean</summary>
    public Boolean EntryEnabled
    {
        get
        {
            if (OrderForm == null) return false;
            return OrderForm.Received == false;
        }
        set
        {
            OnPropertyChanged("EntryEnabled");
        }
    }

    #endregion

当我检查CheckBox时,除了CommodityTemplate中的Textbox控件外,所有TextBox控件都按预期禁用。有人能告诉我我做错了什么。我将不胜感激。

1 个答案:

答案 0 :(得分:0)

我的猜测是EntryEnabledUserControl的视图模型的属性,而不是Commodity类的属性(我猜的是名字)。但在DataTemplate中,CommodityDataContext。这很重要,因为该绑定将在DataContext的{​​{1}}上查找该属性。听起来好像所有其他TextBox都在TextBox中松散运行,所以他们的UserControlDataContext的viewmodel。

从模板内部,您可以使用一些额外的工作绑定到同一个viewmodel:

UserControl