我有一个DependencyObject(一个交互行为),我想从代码中得到它的x:Name(只是get,not set).可能吗?
编辑:关注AnthonyWJones的回答:
我已将以下代码插入到我的基本行为中:
[EditorBrowsable(EditorBrowsableState.Never)]
public string Name
{
get { return (string)GetValue(NameProperty); }
set { SetValue(NameProperty, value); }
}
public static readonly DependencyProperty NameProperty =
DependencyProperty.Register("Name", typeof(string), typeof(BaseBehavior<T>), new PropertyMetadata(null));
Run Code Online (Sandbox Code Playgroud)
我给了我的行为x:Name,但Name属性没有被填充.
我上课了CollectionOfThings.顾名思义,它是一个简单的Thing类实例集合. Thing类有一个公共的默认构造函数和两个简单的公众获取,设置属性ID和DisplayName,两者都是字符串. CollectionOfThing也有公共默认构造函数.
在XAML中我想使用这样的标记: -
<Grid.Resources>
<local:CollectionOfThings x:Key="Things">
<local:Thing ID="1" DisplayName="Hello" />
<local:Thing ID="2" DisplayName="World" />
<local:CollectionOfThings>
</Grid.Resources>
Run Code Online (Sandbox Code Playgroud)
只要CollectionOfThings派生自Collection类型,一切都很好.但是我想CollectionOfThings也是一个DependencyObject.我认为这很好创造的实现ICollection<T>,INotifyCollectionChanged等等并不难.然后我可以从中衍生出来DependencyObject.
不幸的是ICollection<T>,由于某种原因不会削减它.随着ICollection<Thing>我得到'CollectionOfThings不支持Thing作为内容'.回去Collection<Thing>,一切正常,但没有DependencyObject实现让我离开.
有人建议吗?
我创建了空白的 C#/XAML Windows 8 应用程序。添加简单的 XAML 代码:
<Page
x:Class="Blank.MainPage"
IsTabStop="false"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<StackPanel
Margin="0,150"
HorizontalAlignment="Center">
<TextBlock
x:Name="xTitle"
Text="{Binding Title, Mode=TwoWay}"/>
<Button Content="Click me!" Click="OnClick" />
</StackPanel>
</Grid>
</Page>
Run Code Online (Sandbox Code Playgroud)
以及 C# 部分的简单代码:
public sealed partial class MainPage
{
private readonly ViewModel m_viewModel;
public MainPage()
{
InitializeComponent();
m_viewModel = new ViewModel
{
Title = "Test1"
};
DataContext = m_viewModel;
}
private void OnClick(object sender, RoutedEventArgs e)
{
m_viewModel.Title = "Test2";
}
}
Run Code Online (Sandbox Code Playgroud)
现在我想实现ViewModel. 我有两种方法:
c# inotifypropertychanged dependencyobject microsoft-metro windows-8
我很抱歉,因为这太简单了,我知道问题已经得到解答,但在大约 30 页的内容中,我还没有找到我想要解决的问题。
我还没有很好地练习 SL,并尝试编写一个简单版本的文本框,该文本框绑定到屏幕内的属性并在文本更改时更新它,反之亦然(属性更改传播到文本)。由于一些原因,我需要在代码隐藏中使用 DependencyProperties 来执行此操作,而不是在 XAML 中使用 INotifyPropertyChanged。
我最近的尝试看起来像这样:
public partial class MainPage : UserControl
{
static MainPage()
{
TargetTextProperty = DependencyProperty.Register("TargetText", typeof(string), typeof(MainPage), new PropertyMetadata(new PropertyChangedCallback(TextChanged)));
}
public readonly static DependencyProperty TargetTextProperty;
public string TargetText
{
get { return (string)GetValue(TargetTextProperty); }
set { SetValue(TargetTextProperty, value); }
}
public MainPage()
{
InitializeComponent();
TargetText = "testing";
textBox1.DataContext = TargetText;
Binding ResetBinding = new Binding("TargetText");
ResetBinding.Mode = BindingMode.TwoWay;
ResetBinding.Source = TargetText;
textBox1.SetBinding(TextBox.TextProperty, ResetBinding);
}
private static void TextChanged(DependencyObject sender, DependencyPropertyChangedEventArgs …Run Code Online (Sandbox Code Playgroud) 我正在创建一个应该在空XAML文件中使用的自定义时间:
<windows:BlackAndWhiteWindow x:Class="GSdk.Shared.Windows.TestWindow"
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:windows="clr-namespace:GSdk.Shared.Windows"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<windows:AnotherDependencyObject>A string</windows:AnotherDependencyObject>
</windows:BlackAndWhiteWindow>
Run Code Online (Sandbox Code Playgroud)
如何有效地允许我的自定义类BlackAndWhite窗口(继承自DependencyObject和IDisposable)允许接受从visual studio定义的那些"直接内容"?
我正在进行Textbox具有Auto Complete功能的自定义登录.但是,当我尝试:
public static void SetSelectOnMouseOver(DependencyObject obj, bool value)
{
obj.SetValue(ListBoxItemBehavior.SelectOnMouseOverProperty,
(object)(bool)(value ? 1 : 0));
}
Run Code Online (Sandbox Code Playgroud)
我收到错误(object)(bool)(value ? 1 : 0));> cannot convert type 'int' to 'bool',怎么了?
我目前正在处理用户控件并坚持依赖对象类的自定义属性IsEnabled被识别但不是FooText
XAML:
<ScrollViewer VerticalScrollBarVisibility="Hidden" HorizontalScrollBarVisibility="Hidden"
sc:TouchScrolling.IsEnabled = "true"
Grid.Row="0" Grid.Column="1">
Run Code Online (Sandbox Code Playgroud)
我需要在sc:TouchScrolling元素上设置更多属性,但VS一直抱怨它无法找到属性.
TouchScrolling元素继承自Dependency Object
public class TouchScrolling : DependencyObject
{
public bool IsEnabled
{
get { return (bool)GetValue(IsEnabledProperty); }
set { SetValue(IsEnabledProperty, value); }
}
public static readonly DependencyProperty IsEnabledProperty =
DependencyProperty.RegisterAttached("IsEnabled", typeof(bool), typeof(TouchScrolling), new UIPropertyMetadata(false, IsEnabledChanged));
//FooText is not recognized
public string FooText
{
get { return (string)GetValue(FooTextProperty); }
set { SetValue(FooTextProperty, value); }
}
Run Code Online (Sandbox Code Playgroud)