Zdravím, mám UserControl, který renderuju a poté uložím do souboru. Jenže nějak divně tam funguje binding. Když ten usercontrol použiju normálně ve stránce (phone:PhoneApplicationPage), tak to jde, ale když v kódu vytvořím ten můj control, tak to už nefunguje. Tak nějak to vypadá: http://imgup.cz/images/2013/10/05/zx3Sb.... MainPage.xaml
<phone:PhoneApplicationPage
x:Class="UserControlBinding.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="PortraitOrLandscape" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
<TextBlock Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<StackPanel>
<local:MyControl xmlns:local="clr-namespace:UserControlBinding" />
<Button Content="Click to render image" Click="Button_Click" />
<Image x:Name="TestImage" Source="/Assets/ApplicationIcon.png" />
</StackPanel>
</Grid>
</Grid>
</phone:PhoneApplicationPage>
MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using UserControlBinding.Resources;
using System.Windows.Media.Imaging;
namespace UserControlBinding
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
Button_Click(null, null);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
int width = 400;
int height = 400;
MyControl mujControl = new MyControl();
mujControl.Measure(new Size(width, height));
mujControl.Arrange(new Rect(0, 0, width, height));
WriteableBitmap bmp = new WriteableBitmap(width, height);
bmp.Render(mujControl, null);
bmp.Invalidate();
TestImage.Source = bmp;
}
}
}
MyControl.xaml
<UserControl x:Class="UserControlBinding.MyControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
xmlns:local="clr-namespace:UserControlBinding;assembly=UserControlBinding"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="480" d:DesignWidth="480"
x:Name="OmfgControl">
<UserControl.Resources>
<DataTemplate x:Key="jebat">
<Border BorderBrush="White"
BorderThickness="0,1,0,0">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Test1}" />
<TextBlock Text="{Binding Test2}" />
</StackPanel>
</Border>
</DataTemplate>
</UserControl.Resources>
<Grid x:Name="LayoutRoot">
<Grid.Background>
<LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
<GradientStop Color="#FF274D00"/>
<GradientStop Color="#FF66CD00" Offset="1"/>
</LinearGradientBrush >
</Grid.Background>
<StackPanel>
<TextBlock Text="Test" />
<TextBlock Text="{Binding MyItems.Count}" />
<ItemsControl x:Name="OmfgList"
ItemsSource="{Binding MyItems}"
ItemTemplate="{StaticResource jebat}"/>
</StackPanel>
</Grid>
</UserControl>
MyControl.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Collections.ObjectModel;
namespace UserControlBinding
{
public class TestClass
{
public string Test1 { get; set; }
public string Test2 { get; set; }
}
public partial class MyControl : UserControl
{
public ObservableCollection<TestClass> MyItems { get; set; }
public MyControl()
{
InitializeComponent();
MyItems = new ObservableCollection<TestClass>();
MyItems.Add(new TestClass { Test1 = "aaaaaa", Test2 = "AAAA" });
MyItems.Add(new TestClass { Test1 = "bbbbbb", Test2 = "BBBBB" });
MyItems.Add(new TestClass { Test1 = "ccc", Test2 = "CCCC" });
MyItems.Add(new TestClass { Test1 = "ddddd", Test2 = "DDDDDDD" });
MyItems.Add(new TestClass { Test1 = "eeeee", Test2 = "EEEE" });
DataContext = this;
}
}
}
|