Silverlight 4 how to detect the screen size and bind to a grid
I can't get my application to resize the screen correctly. here is my xaml
<Grid x:Name="LayoutRoot"
Background="White"
DataContext="{Binding Source={StaticResource ViewModel}}"
MinHeight="473"
MinWidth="1200"
MaxHeight="600"
MaxWidth="1366">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SizeChanged">
<ei:CallMethodAction MethodName="WndSizeChanged"
TargetObject="{Binding}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="300*" />
<ColumnDefinition Width="189*" />
<ColumnDefinition Width="720*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="101*" />
<RowDefinition Height="372*" />
</Grid.RowDefinitions>
<sdk:DataGrid Grid.Row="1"
Grid.Column="0"
Grid.ColumnSpan="3"
AutoGenerateColumns="False"
HorizontalAlignment="Left"
Height="Auto"
Width="Auto"
Name="dataGrid_Columns"
VerticalAlignment="Top"
ItemsSource="{Binding Path=LstDetails}"
RowDetailsVisibilityMode="VisibleWhenSelected"
IsReadOnly="True"
HeadersVisibility="All"
HorizontalScrollBarVisibility="Visible"
VerticalScrollBarVisibility="Visible"
CanUserReorderColumns="True"
CanUserResizeColumns="True"
CanUserSortColumns="True">
.....more code nested in here.......
</sdk:DataGrid>
I have tried putting auto on the user control, and also in the grid, nothi开发者_如何学Gong is working. If I take out thet min and max, then it goes off the page. That event I put on there, only fires after you resize it. I would assume because this is a user control inside a shell. I am using prism. I found an example to detect the height and width, but it doesn't work initially. When I bind the height and width to double properties, then the event never fires at all. I am running out of ideas. I have found ways to put in code behind the actual width and height. I really want to avoid code behind, but even that did not work. I know the problem has to do with the datagrid, because this is dynamic and will grow or shrink based on filters. Any help is greatly appreciated. Thanks.
This is the best solution I can come up with. It works, but probably not nice since the VM now knows something about the View (correct me if I am wrong). so here is my VM:
public void WndSizeChanged(object sender, EventArgs e)
{
this.setHeightWidth();
}
private void setHeightWidth()
{
this.WindowHeight = (Application.Current.RootVisual as FrameworkElement).ActualHeight - 125;
this.WindowWidth = (Application.Current.RootVisual as FrameworkElement).ActualWidth - 108;
}
public double WindowHeight{get;set;}
public double WindowWidth{get;set;}
and in my view:
<Grid x:Name="LayoutRoot"
Background="White"
DataContext="{Binding Source={StaticResource ViewModel}}"
Height="{Binding Path=WindowHeight, Mode=TwoWay}"
Width="{Binding Path=WindowWidth, Mode=TwoWay}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="LayoutUpdated">
<ei:CallMethodAction MethodName="WndSizeChanged"
TargetObject="{Binding}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="300*" />
<ColumnDefinition Width="189*" />
<ColumnDefinition Width="720*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="101*" />
<RowDefinition Height="372*" />
</Grid.RowDefinitions>
Basically changed the event that is triggered to be LayoutUpdated. Works like a charm. I am on a time-line so this will have to do. I hope this will help anyone who is in a similar situation as me. Don't forget to call setHeightWidth() in the constructor to set the h and w initially as well.
精彩评论