[英]TwoWay Binding with UserControl
我試圖在我創建的UserControl上設置twoway綁定。
當我在Xaml中使用控件時,設置DataContext就像這樣......
<uc:MyUserControl DataContext="{Binding Path=MyObject, Mode=TwoWay}" />
我的用戶控件定義如下....
<UserControl x:Class="SilverlightApplication1.XText"
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"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<TextBox x:Name="Text" Text="{Binding}"/>
</Grid>
</UserControl>
數據顯示正確,但如果我改變,我希望它用TwoWay綁定更新。
我在下面嘗試了這個,但它在運行時出錯,因為沒有定義Path。
<Grid x:Name="LayoutRoot" Background="White">
<TextBox x:Name="Text" Text="{Binding Mode=TwoWay}"/>
</Grid>
</UserControl>
關於如何讓usercontrol中的控件雙向綁定到DataContext的任何想法?
雖然您的上述(自我回答)答案似乎解決了問題,但我不禁認為這是一個問題域問題。 我很難想到為什么你想要首先直接綁定,特別是因為它讓你對數據的變化控制較少。
請考慮以下事項:
<UserControl
x:Class="SilverlightApplication1.XText"
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"
x:Name="UserControl"
d:DesignHeight="300"
d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<TextBox x:Name="Text" Text="{Binding Path=Value, ElementName=UserControl, Mode=TwoWay}"/>
</Grid>
</UserControl>
然后在代碼隱藏中:
public partial class XText
{
public static DependencyProperty ValueProperty =
DependencyProperty.Register(
"Value",
typeof(string),
typeof(XText),
new FrameworkPropertyMetadata(null)
);
public string Value
{
get { return ((string)(base.GetValue(XText.ValueProperty))); }
set { base.SetValue(XText.ValueProperty, value); }
}
...
}
然后,當你准備好使用它時:
<uc:XText Value="{Binding Path=MyObject, Mode=TwoWay}" />
是的,這是更多的代碼,但是它可以讓你更好地控制UserControl
里面的Value
會發生什么,並且在將來使這個代碼更加簡單。
思考?
-Doug
編輯:修復了幾個拼寫錯誤。
我找到了一個不需要你給基本控件命名的解決方案。 當我為基本UserControl定義一個名稱時,我在向網格添加多個實例時為我創建了問題,因為它們被定義為相同的名稱。
這是我的第一個答案和Doug的答案的組合。 請注意,UserControl缺少name屬性,TextBox沒有在XAML中聲明Binding
XAML
<UserControl
x:Class="SilverlightApplication1.XText"
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"
d:DesignHeight="300"
d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<TextBox x:Name="MyText"/>
</Grid>
</UserControl>
代碼隱藏
public partial class XText
{
public XText()
{
InitializeComponent();
MyText.SetBinding(TextBox.TextProperty, new Binding()
{
Source = this,
Path = new PropertyPath("Value"),
Mode = BindingMode.TwoWay
});
}
public static DependencyProperty ValueProperty =
DependencyProperty.Register(
"Value",
typeof(string),
typeof(XText),
new PropertyMetadata(null)
);
public string Value
{
get { return ((string)(GetValue(ValueProperty))); }
set { SetValue(ValueProperty, value); }
}
...
}
准備使用時,請執行以下操作
<uc:XText Value="{Binding Path=MyObject, Mode=TwoWay}" />
好吧,我想我已經想出辦法讓這個工作......
首先,我在我的UserControl代碼中設置了一個公共屬性...
public Binding BindingValue
{
set { this.MyTextBox.SetBinding(TextBox.TextProperty, value); }
}
然后在XAML中
<uc:MyUserControl BindingValue="{Binding Path=MyObject, Mode=TwoWay}" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.