我尝试使用附加属性绑定.但无法让它发挥作用.
public class Attached
{
public static DependencyProperty TestProperty =
DependencyProperty.RegisterAttached("TestProperty", typeof(bool), typeof(Attached),
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault | FrameworkPropertyMetadataOptions.Inherits));
public static bool GetTest(DependencyObject obj)
{
return (bool)obj.GetValue(TestProperty);
}
public static void SetTest(DependencyObject obj, bool value)
{
obj.SetValue(TestProperty, value);
}
}
Run Code Online (Sandbox Code Playgroud)
XAML代码:
<Window ...>
<StackPanel local:Attached.Test="true" x:Name="f">
<CheckBox local:Attached.Test="true" IsChecked="{Binding (local:Attached.Test), Mode=TwoWay, RelativeSource={RelativeSource Self}}" />
<CheckBox local:Attached.Test="true" IsChecked="{Binding (local:Attached.Test), Mode=TwoWay}" />
</StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)
和绑定错误:
System.Windows.Data Error: 40 : BindingExpression path error: '(local:Attached.Test)' property not found on 'object' ''StackPanel' (Name='f')'. BindingExpression:Path=(local:Attached.Test); DataItem='StackPanel' (Name='f'); …Run Code Online (Sandbox Code Playgroud) 我如何以编程方式执行以下操作(来自XAML):
<TextBox Name="OrderDateText"
Text="{Binding Path=OrderDate, StringFormat=dd-MM-yyyy}"
public DateTime OrderDate
Run Code Online (Sandbox Code Playgroud)
现在我有以下内容
TextBox txtboxOrderdDate = new TextBox();
Run Code Online (Sandbox Code Playgroud)
而且我知道我需要做类似的事情
Binding bindingOrderDate = new Binding();
bindingOrderDate.Source = "OrderDate";
Run Code Online (Sandbox Code Playgroud)
但我被困在这里......不知道如何继续应用StringFormat,我也不确定SOURCE是正确的方法(我应该使用ElementName吗?)