๋ฌธ์์ด์ ObservableCollection์ ๋ฐ์ธ๋ฉ๋ ListView๊ฐ ์์ต๋๋ค. Listview์๋ ํ๋์ ๋ ์ด๋ธ๊ณผ ํ๋์ ContentView(๋ ์ด๋ธ๋ง ํฌํจ)๊ฐ ์์ต๋๋ค. ๋ ๋ค ๋์ผํ ์ปฌ๋ ์ ์ ๋ฐ์ธ๋ฉ๋ฉ๋๋ค.
๋ํ ์ปฌ๋ ์ ์ ๋ํ ์์์ ๋ฐ์ดํฐ๋ฅผ ์์ฑํ๋ ๋ฒํผ์ด ์์ต๋๋ค.
๋ฌธ์ ๋ ์ฑ์ ์คํํ๊ณ ๋ฐ์ดํฐ ์์ฑ ๋ฒํผ์ ํด๋ฆญํ๋ฉด ๋ ์ด๋ธ์ด ์ ๋ฐ์ดํธ๋์ง๋ง ContentView๋ ์ ๋ฐ์ดํธ๋์ง ์๋๋ค๋ ๊ฒ์ ๋๋ค.
์ฝํ ์ธ ๋ณด๊ธฐ ๋ด๋ถ์ ๋ ์ด๋ธ๋ ๋ณ๊ฒฝ๋์ด์ผ ํฉ๋๋ค.
์ฝํ ์ธ ๋ณด๊ธฐ ๋ด๋ถ์ ๋ ์ด๋ธ์ ๋์ผํ๊ฒ ์ ์ง๋ฉ๋๋ค.
๋๊ฒ ํจํค์ง:
Xamarin.Forms
NETStandard.Library
์ํฅ์ ๋ฐ๋ ์ฅ์น:
์ผ์ฃผ์ผ ์ ์ stackoverflow์ ๋ํ ์ง๋ฌธ - https://stackoverflow.com/questions/50211197/xamarin-forms-user-control-binding-inside-listview
ํ์
this.BindingContext = this;
MagicBox.xaml.cs์์ BindingContext๋ฅผ ํ์ฌ ๊ฐ์ฒด๋ก ๊ฐ์ ์ ์ฉํฉ๋๋ค. ๋ํ ๋ถ๋ชจ์ BindingContext๊ฐ ๋ ์ด์ ์์๋์ง ์์์ ์๋ฏธํฉ๋๋ค.
์๋ํ๊ฒ ํ๋ ค๋ฉด ์ฝ๋๋ฅผ ๋ค์์ผ๋ก ๋ณ๊ฒฝํ์ญ์์ค.
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MagicBox : ContentView
{
public static readonly BindableProperty TextProperty =
BindableProperty.Create("Text", typeof(string), typeof(MagicBox), default(string));
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public MagicBox ()
{
InitializeComponent ();
}
}
๊ทธ๋ฆฌ๊ณ ๋น์ ์ xaml์
<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TestSample.Controls.MagicBox"
x:Name="box">
<ContentView.Content>
<Grid>
<Label Text="{Binding Text, Source={x:Reference box}}" />
</Grid>
</ContentView.Content>
</ContentView>
@StephaneDelcroix ๊ฐ์ฌํฉ๋๋ค. ์๋ํ์ต๋๋ค :)
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
ํ์
MagicBox.xaml.cs์์ BindingContext๋ฅผ ํ์ฌ ๊ฐ์ฒด๋ก ๊ฐ์ ์ ์ฉํฉ๋๋ค. ๋ํ ๋ถ๋ชจ์ BindingContext๊ฐ ๋ ์ด์ ์์๋์ง ์์์ ์๋ฏธํฉ๋๋ค.
์๋ํ๊ฒ ํ๋ ค๋ฉด ์ฝ๋๋ฅผ ๋ค์์ผ๋ก ๋ณ๊ฒฝํ์ญ์์ค.
๊ทธ๋ฆฌ๊ณ ๋น์ ์ xaml์