Get control properties from Grid.Resources in code behind
At the begining, theres xaml code:
<Grid.Resources>
<DataTemplate x:Name="dataTemp" x:Key="dtKey">
<WrapPanel Orientation="Horizontal" Name="mainWP">
<TextBlock Name="codeTB" FontSize="18" Width="200" Text="{Binding barcode}"></TextBlock>
(...)
</WrapPanel>
</DataTemplate>
</Grid.Resources>
and listview with datatemplate:
<ListView Name="testLV" Grid.Row="0" ItemTemplate="{StaticResource ResourceKey=dtKey}" >
</ListView>
So in code behind i'd like to change TextBlock width as this.width/5 (becouse width could be different in another PC), but becouse it's DataTemplate i don't have access to this control. I also tried Width="{Binding Path=ActualWidth, ElementName=grid0}", but as actual width i need something like ActualWidth/5, which doesnt work
Thanks
Using a Grid
with 5 columns and each with Width="0.2*"
will work fine when all the child element's DesiredWidth
have been satisfied(In other words when the size of the Grid is large enough to fit all columns with equal space). If it cannot do this, The layout works in a way to trim elements it can and give the extra space to other columns that need it more thereby overriding the Width="0.2*"
in the process
For your requirement where you want the 5 columns to be split equally, just use a UniformGrid
. That pretty much doesn't care abt any of the above things.
So say something like:
<ListView Name="paragonLV" HorizontalContentAlignment="Stretch">
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="Padding"
Value="0" />
<Setter Property="BorderThickness"
Value="0" />
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<UniformGrid MaxWidth="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type ScrollViewer}},
Path=ActualWidth}"
Columns="5">
<UniformGrid.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="TextTrimming"
Value="CharacterEllipsis" />
<Setter Property="FontSize"
Value="18" />
<Setter Property="HorizontalAlignment"
Value="Stretch" />
</Style>
</UniformGrid.Resources>
<TextBlock Text="{Binding barCode}" />
<TextBlock Text="{Binding nazwa}" />
<TextBlock Text="{Binding jm}" />
<TextBlock Text="{Binding ilosc}" />
<TextBlock Text="{Binding cena}" />
</UniformGrid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Use Grid.Columndefination to format your grid or otherwise use Ivaluconverter class let's see value converter development
convert parameter will your calculating parameter, you know how to construct value converter class
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
//value is grid actual width
// parameter = 5 is your calculated value
return value / parameter;
}
链接地址: http://www.djcxy.com/p/44600.html