在silverlight中将rtf转换为xaml

时间:2013-05-16 12:39:54

标签: c# silverlight-4.0

我想使用RichTextBox控件在silverlight中加载rtf文件,但此控件不会以正确的格式加载文件。任何人都可以告诉如何这样做或将rtf转换为xaml?

这是xaml:

    <Grid x:Name="LayoutRoot" Height="480" Width="640">
            <RichTextBox HorizontalAlignment="Left" Margin="89,64,0,0" Name="rtb" VerticalAlignment="Top" Height="301" Width="416"/>
            <Button Content="Select File" Height="23" HorizontalAlignment="Left" Margin="268,12,0,0" Name="btnSelect" VerticalAlignment="Top" Width="75" Click="btnSelect_Click" />
            <TextBlock Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="textBlock1" Text="FileName:" VerticalAlignment="Top" Width="73" />
            <TextBox Height="23" HorizontalAlignment="Left" Margin="89,12,0,0" Name="txtFile" VerticalAlignment="Top" Width="164" />
            <Button Content="Reset" Height="23" HorizontalAlignment="Left" Margin="430,385,0,0" Name="btnReset" VerticalAlignment="Top" Width="75" Click="btnReset_Click" IsEnabled="False" />
        </Grid>


Code behind :

private void btnSelect_Click(object sender, RoutedEventArgs e)
        {

            OpenFileDialog odlg = new OpenFileDialog();

            odlg.Filter = "Text files|*.rtf";
            odlg.Multiselect = false;
            string contents = null;

            if ((bool)odlg.ShowDialog())
            {
                txtFile.Text = odlg.File.Name;

                StreamReader reader = new StreamReader(odlg.File.OpenRead());
                while (!reader.EndOfStream)
                {
                    contents = reader.ReadToEnd();
                }
                reader.Close();

                rtb.Selection.Text = contents;
                btnReset.IsEnabled = true;
            }
        }

        private void btnReset_Click(object sender, RoutedEventArgs e)
        {
            txtFile.Text = "";
            rtb.Blocks.Clear();
        }

0 个答案:

没有答案