如何将listboxitem的高度调整为ViewBox中的TextBox

时间:2019-02-06 16:15:17

标签: c# wpf viewbox

在我的WPF表单上,我在ViewBox和ListBox内都有TextBox。我希望ListBoxItem的高度始终与TextBox相同。问题是如何实现这一目标。

我试图设置ListBoxItem模板的属性,但是不起作用

 <Setter Property="Height" Value="{Binding ElementName=SearchBox, Path=ActualHeight, UpdateSourceTrigger=PropertyChanged}" />

这是我的完整代码

<UserControl x:Class="MApp.Views.ItemStockView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
         xmlns:b7mControls="clr-namespace:BccSboApps.MetroNext.Controls;assembly=BccSboApps.MetroNext"            
         xmlns:b7mToolbox="clr-namespace:BccSboApps.MetroNext.Toolbox;assembly=BccSboApps.MetroNext"
         xmlns:VisLib="clr-namespace:MobileWhs.VisualControls" 
         xmlns:myTool="clr-namespace:MobileWhs.Toolbox"
         d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Styles/Styles.xaml" />
        </ResourceDictionary.MergedDictionaries>
        <b7mToolbox:PercentageConverter x:Key="PercentageConverter" />
        <Style x:Key="AlternatingListBoxItemStyle" TargetType="{x:Type ListBoxItem}">
            <Setter Property="Height" Value="{Binding ElementName=SearchBox, Path=ActualHeight, UpdateSourceTrigger=PropertyChanged}" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <Border
                            Name="_Border"
                            Padding="2"
                            SnapsToDevicePixels="true">
                            <ContentPresenter />
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                                <Setter TargetName="_Border" Property="Background" Value="#FF0f243E" />
                            </Trigger>
                            <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                                <Setter TargetName="_Border" Property="Background" Value="#FF576577" />
                            </Trigger>
                            <Trigger Property="IsSelected" Value="true">
                                <Setter TargetName="_Border" Property="Background" Value="OrangeRed" />
                                <Setter Property="Foreground" Value="White" />
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter TargetName="_Border" Property="Background" Value="OrangeRed" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>


        <DataTemplate x:Key="ListBoxTemplateViewBox">
            <Grid x:Name="__PART_GRID">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="5*" />
                    <ColumnDefinition Width="2*" />
                </Grid.ColumnDefinitions>

                <TextBlock Grid.Column="0" Grid.Row="0" 
                           Background="Beige"
                           Text="{Binding Path=Title}" />

                <TextBlock Grid.Column="1" Grid.Row="0" 
                           Height="{Binding ElementName=SearchBox, Path=ActualHeight}"
                           Text="{Binding Path=RightTop}" />

                <TextBlock Grid.Column="0" Grid.Row="1"  
                           Height="{Binding ElementName=SearchBox, Path=ActualHeight}"
                           Text="{Binding Path=SecondLine}" />

                <TextBlock Grid.Column="1" Grid.Row="1"
                           Height="{Binding ElementName=SearchBox, Path=ActualHeight}"
                           Text="{Binding Path=RightBottom}" />

            </Grid>
            <DataTemplate.Triggers>
                <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                    <Setter TargetName="__PART_GRID" Property="Background" Value="#FF0f243E" />
                </Trigger>
                <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                    <Setter TargetName="__PART_GRID" Property="Background" Value="#FF576577" />
                </Trigger>
            </DataTemplate.Triggers>
        </DataTemplate>

        <myTool:DictionaryConverter x:Key="DictionaryConverter" />
        <myTool:NullToVisibilityConverter x:Key="NullToVisibilityConverter" />
    </ResourceDictionary>
</UserControl.Resources>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="10*" />
        <RowDefinition Height="20*" />
        <RowDefinition Height="70*" />
    </Grid.RowDefinitions>
    <VisLib:ViewboxPanel Grid.Column="0" Grid.Row="0" >
        <TextBox x:Name="SearchBox" 
                 Text="{Binding Path=SearchExpr, UpdateSourceTrigger=PropertyChanged}" 
                 Controls:TextBoxHelper.Watermark="{Binding SboSession.ML, Converter={StaticResource DictionaryConverter},ConverterParameter=Find}"  />
    </VisLib:ViewboxPanel>
    <VisLib:ViewboxPanel Grid.Column="0" Grid.Row="1" >
        <TextBlock TextWrapping="Wrap" 
                   Text="{Binding Path=ItemDescription, UpdateSourceTrigger=PropertyChanged}" />
    </VisLib:ViewboxPanel>
    <ListBox Grid.Column="0" Grid.Row="2" HorizontalContentAlignment="Stretch"
            x:Name="lstOrderItems"
            AlternationCount="2"
            ItemTemplate="{DynamicResource ListBoxTemplateViewBox}"
            ItemsSource="{Binding Path=ItemStockList}"
            ScrollViewer.HorizontalScrollBarVisibility="Disabled"
            ScrollViewer.VerticalScrollBarVisibility="Auto"
            SelectedItem="{Binding Path=SelectedItem}">
        <ListBox.ItemContainerStyle>
            <Style BasedOn="{StaticResource AlternatingListBoxItemStyle}" TargetType="ListBoxItem">
                <EventSetter Event="PreviewMouseDown" Handler="ItemOnPreviewMouseDown" />
            </Style>
        </ListBox.ItemContainerStyle>
    </ListBox>
</Grid>

0 个答案:

没有答案