资源样式在数据模板中不起作用



我有以下资源:

<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>

在我的控件中,我有许多文本框控件。

然后我有下面的lieview控件,它利用了CategoryTemplate。这个模板包含了另一个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}" />

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

    #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

当我选中复选框时,所有的文本框控件都像预期的那样禁用,除了在CommodityTemplate中找到的文本框控件。谁能告诉我我哪里做错了。我将不胜感激。

我的猜测是EntryEnabledUserControl的视图模型的属性,而不是Commodity类的属性(我猜名字)。但在DataTemplate中,Commodity就是DataContext。这很重要,因为绑定将在TextBoxDataContext上查找属性。这听起来像你所有的其他文本框到处乱跑在UserControl,所以他们的DataContextUserControl的视图模型。

从模板内部,您可以绑定到相同的视图模型,只需要做一点额外的工作:

Value="{Binding DataContext.EntryEnabled, RelativeSource={RelativeSource AncestorType=UserControl}}"

相关内容

  • 没有找到相关文章

最新更新