Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Get Token value from data template items

$
0
0
I am tryin to get Datacontrol value (TextBlock) from code behind

I have xaml like 

<code>
    <UserControl                 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"                  xmlns:ViewModel="clr-namespace:SDKTeam.DLKCMailer.ViewModel" x:Class="SDKTeam.DLKCMailer.Control.MailControl"                  mc:Ignorable="d"                  d:DesignHeight="300" d:DesignWidth="600" Background="#EBEBEB">        <UserControl.Resources>            <DataTemplate x:Key="NameTokenTemplate">                <DataTemplate.Resources>                    <Storyboard x:Key="OnLoaded1">                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="border">                            <SplineDoubleKeyFrame KeyTime="0" Value="0"/>                            <SplineDoubleKeyFrame KeyTime="0:0:0.5" Value="1"/>                        </DoubleAnimationUsingKeyFrames>                    </Storyboard>                </DataTemplate.Resources>                <Border x:Name="border" BorderBrush="#FF7E7E7E" BorderThickness="0.5" CornerRadius="5" Height="Auto" VerticalAlignment="Center"  d:DesignHeight="10"
 Margin="3,3,3,3">                    <Grid HorizontalAlignment="Left" Width="Auto">                        <!--<Grid.ColumnDefinitions>                            <ColumnDefinition Width="0.21*"/>                            <ColumnDefinition Width="0.79*"/>                        </Grid.ColumnDefinitions>-->                        <TextBlock x:Name="tbEmailAddress" TextWrapping="NoWrap" Text="{Binding}" Width="Auto" VerticalAlignment="Center" HorizontalAlignment="Center"
 Grid.Column="0" Margin="5,0,5,0" FontWeight="Normal"/>                        <!--<Image HorizontalAlignment="Right" Source="..\Resources\Image\close.png" Margin="5" MaxWidth="1" MaxHeight="1" Stretch="None"
 Width="Auto" Grid.Column="1" VerticalAlignment="Center"/>-->                    </Grid>                </Border>                <DataTemplate.Triggers>                    <EventTrigger RoutedEvent="FrameworkElement.Loaded">                        <BeginStoryboard Storyboard="{StaticResource OnLoaded1}"/>                    </EventTrigger>                </DataTemplate.Triggers>            </DataTemplate>        </UserControl.Resources>        <Grid>            <Grid.RowDefinitions>                <RowDefinition Height="44*"/>                <RowDefinition Height="51*"/>                <RowDefinition Height="116*"/>                <RowDefinition Height="89*"/>            </Grid.RowDefinitions>            <Grid.ColumnDefinitions>                <ColumnDefinition Width="120"/>                <ColumnDefinition Width="159*"/>            </Grid.ColumnDefinitions>            <Label Content="E-Mail Alias (*)" HorizontalAlignment="Left" Margin="29,10,0,0" VerticalAlignment="Top" Height="26" Width="87"/>            <Label Content="E-Mail Subject (*)" HorizontalAlignment="Left" Margin="19,10,0,0" VerticalAlignment="Top" Grid.Row="1" Height="26" Width="101"/>            <TextBox x:Name="tbMailSubject" Grid.Column="1" HorizontalAlignment="Stretch"  Margin="10,10,10,15" Grid.Row="1" VerticalAlignment="Stretch"/>            <StackPanel Orientation="Horizontal" Width="Auto" Grid.Row="3" Grid.ColumnSpan="2" HorizontalAlignment="Stretch" Margin="0,10,10,41">            <Button x:Name="cbAttachedFile" Cursor="Hand" Content="Send checked file(s) to E-mail as attachment" HorizontalAlignment="Stretch" Margin="10,0,0,0"  VerticalAlignment="Top"
 Grid.ColumnSpan="2"  Click="cbAttachedFile_Click" Width="289" Height="30" />            <Button x:Name="cbEmailLink" Cursor="Hand" Content="Copy checked file(s) to share and email the link(s)" HorizontalAlignment="Stretch" Margin="10,0,0,0"  VerticalAlignment="Top" Click="cbEmailLink_Click" Grid.Column="1" Width="275" Height="30" />            </StackPanel>            <RichTextBox x:Name="rbMessageBody" HorizontalAlignment="Stretch"  Margin="10,10,10,10" Grid.Row="2" VerticalAlignment="Stretch"  Grid.ColumnSpan="2">                <FlowDocument>                    <Paragraph>                        <Run/>                    </Paragraph>                </FlowDocument>            </RichTextBox>                <ViewModel:MailAddressToken x:Name="Tokenizer" Grid.Column="1" HorizontalAlignment="Stretch" Margin="10,10,10,10" TokenTemplate="{DynamicResource NameTokenTemplate}"
 VerticalScrollBarVisibility="Auto" VerticalAlignment="Stretch" IsManipulationEnabled="True">                <FlowDocument>                    <Paragraph>                        <Run/>                    </Paragraph>                </FlowDocument>            </ViewModel:MailAddressToken>            </Grid>    </UserControl>



</code>

and custom control like 

<code>

   
 public class MailAddressToken :RichTextBox        {            public static readonly DependencyProperty TokenTemplateProperty =                DependencyProperty.Register("TokenTemplate", typeof(DataTemplate), typeof(MailAddressToken));                public DataTemplate TokenTemplate            {                get { return (DataTemplate) GetValue(TokenTemplateProperty); }                set { SetValue(TokenTemplateProperty, value); }            }                public Func<string, object> TokenMatcher { get; set; }                             public MailAddressToken()            {                TextChanged += OnTokenTextChanged;            }                    private void OnTokenTextChanged(object sender, TextChangedEventArgs e)            {                var text = CaretPosition.GetTextInRun(LogicalDirection.Backward);                if (TokenMatcher != null)                {                    var token = TokenMatcher(text);                    if (token != null)                    {                        ReplaceTextWithToken(text, token);                    }                }            }                private void ReplaceTextWithToken(string inputText, object token)            {                // Remove the handler temporarily as we will be modifying tokens below, causing more TextChanged events                TextChanged -= OnTokenTextChanged;                    var para = CaretPosition.Paragraph;                    var matchedRun = para.Inlines.FirstOrDefault(inline =>                {                    var run = inline as Run;                    return (run != null && run.Text.EndsWith(inputText));                }) as Run;                if (matchedRun != null) // Found a Run that matched the inputText                {                    var tokenContainer = CreateTokenContainer(inputText, token);                    para.Inlines.InsertBefore(matchedRun, tokenContainer);                        // Remove only if the Text in the Run is the same as inputText, else split up                    if (matchedRun.Text == inputText)                    {                        para.Inlines.Remove(matchedRun);                    }                    else // Split up                    {                        var index = matchedRun.Text.IndexOf(inputText) + inputText.Length;                        var tailEnd = new Run(matchedRun.Text.Substring(index));                        para.Inlines.InsertAfter(matchedRun, tailEnd);                        para.Inlines.Remove(matchedRun);                    }                }                    TextChanged += OnTokenTextChanged;            }                private InlineUIContainer CreateTokenContainer(string inputText, object token)            {                // Note: we are not using the inputText here, but could be used in future                    var presenter = new ContentPresenter()                {                    Content = token,                    ContentTemplate = TokenTemplate,                };                    // BaselineAlignment is needed to align with Run                return new InlineUIContainer(presenter) { BaselineAlignment = BaselineAlignment.TextBottom };            }                    public object OnTokendelete { get; set; }        }


</code>


This will create token template of TextBlock

I want to retrieve all txtblock items in code Behind of usercontrol.

Also is there way if i delete block of item (Token item) i can get which token item is deleted.

asthanarht



Viewing all articles
Browse latest Browse all 18858

Trending Articles