我试图通过XAML绑定依赖属性到我的自定义WPF控件.
这是我如何注册依赖属性:
public static readonly DependencyProperty AltNamesProperty =
DependencyProperty.Register ("AltNames", typeof(string), typeof(DefectImages));
public string AltNames
{
get { return (string) GetValue(AltNamesProperty); }
set { SetValue(AltNamesProperty, value); }
}
Run Code Online (Sandbox Code Playgroud)
以下是我在XAML中调用它的方式:
<DataGrid.Columns>
<DataGridTemplateColumn IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Name="StackPanel1" Grid.Column="0" Width="950">
<TextBlock FontSize="16" TextDecorations="None" Text="{BindingPath=StandardName}" Foreground="Black" FontWeight="Bold" Padding="5,10,0,0"></TextBlock>
<TextBlock Text="{Binding Path=AltNames}"TextWrapping="WrapWithOverflow" Padding="5,0,0,10"></TextBlock>
<!-- this part should be magic!! -->
<controls:DefectImages AltNames="{Binding Path=AltNames}"></controls:DefectImages>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
Run Code Online (Sandbox Code Playgroud)
我知道AltNames我试图绑定的属性是一个有效的属性,因为我可以在文本块中显示它就好了.我是否错误地注册了Dependency属性?
我需要做什么才能AltNames在我的代码中分配正确的值?
我需要能够调用一个方法并传入一个未知类型的对象,但然后调用正确的重载.我还需要一个接受object作为参数类型的默认实现
.我所看到的是默认的重载是唯一被使用过的.
这是我要做的事情的要点:
class Formatter
{
private object Value;
public Formatter(object val){
Value = val;
}
public override string ToString()
{
return Format(Value);
}
private string Format(object value)
{
return value.ToString();
}
private string Format(DateTime value)
{
return value.ToString("yyyyMMdd");
}
}
Run Code Online (Sandbox Code Playgroud)
好的,到目前为止一切顺利.现在我希望能够做到这一点:
public static class FancyStringBuilder()
{
public static string BuildTheString()
{
var stringFormatter = new Formatter("hello world");
var dateFormatter = new Formatter(DateTime.Now);
return String.Format("{0} {1}", stringFormatter, dateFormatter);
}
}
Run Code Online (Sandbox Code Playgroud)
结果FancyStringBuilder.BuildTheString()是"hello world 2012-12-21 00:00:00.000",当我预料到的时候 …
我想以编程方式生成带有纯色填充的矩形图像.我怎样才能做到这一点?我想用inputImage填充inputImageView.
到目前为止,这是我的代码,但我得到的只是一个空图像视图:
-(void) awakeFromNib{
CIColor *red = [CIColor colorWithRed:1 green:0 blue:0];
CIImage *redImage = [CIImage imageWithColor:red];
NSCIImageRep *ir = [NSCIImageRep imageRepWithCIImage:redImage];
[self setInputImage:[[NSImage alloc] initWithSize: NSMakeSize(25, 25)]];
//magic should be happening right here!
[[self inputImage] addRepresentation:ir];
[[self inputView] setImage:[self inputImage]];
[inputView setNeedsDisplay:YES];
}
Run Code Online (Sandbox Code Playgroud)