The problem is that when you call UserControl in DataTemplate from an ItemSource such as ListView, Binding does not work.
The UserControls I created are as follows:
MyUserControl1.cs
namespace EditableTextBox
{
public sold partial class MyUserControl1: UserControl
{
public string text
{
get
{
return(string)GetValue(TextProperty);
}
set
{
SetValue (TextProperty, value);
}
}
public static readonly DependencyProperty TextProperty=
DependencyProperty.Register("Text", typeof(string), typeof(MyUserControl1), new PropertyMetadata(null));
public MyUserControl1()
{
This.InitializeComponent();
This.DataContext=this;
}
private void TextBlock_Tapped (object sender, TappedRoutedEventArgse)
{
Block.Visibility=Visibility.Collapsed;
Box.Visibility=Visibility.Visible;
}
private void Box_LostFocus(object sender, RoutedEventArgse)
{
Block.Visibility=Visibility.Visible;
Box.Visibility=Visibility.Collapsed;
}
}
}
MyUsercontrol1.xaml
<UserControl
x: Class="EditableTextBox.MyUserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns: x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:EditableTextBox"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns: mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc —Ignorable="d"
d: DesignHeight="300"
d: DesignWidth = "400" >
<Grid>
<TextBlock Visibility="Visible" Name="Block" Text="{Binding Text, Mode=TwoWay}"Tapped="TextBlock_Tapped"/>
<TextBox Visibility="Collapsed" Name="Box" Text="{Binding Text, Mode=TwoWay}"LostFocus="Box_LostFocus"/>
</Grid>
And here's the MainPage.xaml to call UserControl:
MainPage.xaml.cs
namespace EditableTextBox
{
/// <summary>
/// A blank page that can be used by itself or moved into a frame.
/// </summary>
public sold partial class MainPage:Page
{
publicMainPage()
{
This.InitializeComponent();
test = "test";
test2 = new List<string>();
test2.Add("1");
test2.Add("2");
This.DataContext=this;
}
public string test {get;set;}
public List <string > test2 {get; set;}
}
}
MainPage.xaml
<Page
x: Class="EditableTextBox.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns: x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:EditableTextBox"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns: mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc —Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<local:MyUserControl1Text="{x:Bind test,Mode=TwoWay}"HorizontalAlignment="Left" Margin="304,227,0,0"VerticalAlignment="Top" Width="568"Height="35"/gt;
<ListView ItemsSource="{x:Bind test2,Mode=TwoWay}" HorizontalAlignment="Left" Height="416" Margin="309,380,0,0" VerticalAlignment="Top" Width="617">
<ListView.ItemTemplate>
<DataTemplate x:DataType="x:String">
<local:MyUserControl1Text="{Binding}">/local:MyUserControl1>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
Originally
I'd like to display it as .
The current code is
appears as shown in .
Please let me know.
Try removing this.DataContext=this;
from the user control.
DataContext just seems to be unintended.
© 2024 OneMinuteCode. All rights reserved.