我创建了一个mycustomItemsPanel在App.Resources
<Application.Resources>
<ItemsPanelTemplate x:Key="mycustomItemsPanel">
.... Some code here
</ItemsPanelTemplate>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
并以这种方式将其提供给UIControl
<.... ItemsPanel="{StaticResource mycustomItemsPanel}" />
Run Code Online (Sandbox Code Playgroud)
但我知道这可以作为提供
<.... ItemsPanel="Binding Source={StaticResource mycustomItemsPanel}}" />
Run Code Online (Sandbox Code Playgroud)
这些有什么区别?
我有一个XML文件
<? xml version="1.0" encoding="utf-8"?>
<resources>
<string name="Foo">Bar</string>
<string name="Foo1">Bar1</string>
// More string Tags here
</resources>
Run Code Online (Sandbox Code Playgroud)
我试过了
XMLTextReader reader = new XmlTextReader("FooBar.xml");
ResXResourceWriter writer = new ResXResourceWriter("FooBar.resx");
while(reader.Read())
{
if(reader.NodeType == XmlNodeType.Element && reader["name"] != null)
writer.AddResource("What_should_I_write_here", "What_should_I_write_here");
}
Run Code Online (Sandbox Code Playgroud)
如何阅读这个xml,以便我可以创建一个resx文件.
我从这里下载了 TreeListView 。当数据被剪切时,它没有显示水平或垂直滚动条。像这样

所以我把它的 Style 改为
<Style TargetType="{x:Type l:TreeListView}">
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility"
Value="Auto" />
<Setter Property="ScrollViewer.VerticalScrollBarVisibility"
Value="Auto" />
<Setter Property="VerticalContentAlignment"
Value="Center" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type l:TreeListView}">
<Border BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<DockPanel>
<GridViewHeaderRowPresenter Columns="{StaticResource gvcc}"
DockPanel.Dock="Top" />
<ScrollViewer x:Name="_tv_scrollviewer_"
Background="{TemplateBinding Background}"
CanContentScroll="False"
Focusable="True"
HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}"
Padding="{TemplateBinding Padding}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}">
<ItemsPresenter />
</ScrollViewer>
</DockPanel>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="VirtualizingStackPanel.IsVirtualizing"
Value="true">
<Setter Property="CanContentScroll"
TargetName="_tv_scrollviewer_"
Value="true" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)
垂直滚动条很好。但问题是水平 Scollbar。当数据被水平裁剪,滚动条向右移动时,数据向右移动,但标题保持原位。像这样。

如何解决水平滚动 treeitem 时标题随之移动的问题。我不允许在滚动查看器中放置标题,因为在垂直滚动数据时它们需要可见。
我知道我们可以通过以下两种方式之一初始化数组:
Enumerable.Repeat(可能导致内存问题?链接:Enumerable.Repeat有一些内存问题?)我有两个问题:
我们可以用Array.ForEach这种方式初始化数组吗?
double[][] myarr = new double[13][];
Array.ForEach(myarr, *enter_code_here*);
Run Code Online (Sandbox Code Playgroud)
我尝试替换enter_code_here为:
s => s = new double[2];
Run Code Online (Sandbox Code Playgroud)
但它没有初始化myarr.
没有任何内存问题的其他数组初始化方法是什么?
我自己是LINQ的忠实粉丝.LINQ反映了简洁的艺术.同样在.Net中我们说LINQ比循环领先一步,或者我们可以说LINQ是循环++.
但这真的是这样吗?
为什么我的判断是因为我试图将这个转换为循环代码转换为LINQ但是混淆了LINQ跳过/离开索引?
double[] NMD = {3.0, 5.0, 6.0, 65.0, 34.0, 3.0, 5.0, 6.0, 65.0, 34.0, 3.0, 5.0, 6.0, 65.0, 34.0 };
for(int i=1; i<NMD.Length-1; i+=2)
NMD[i] = NMD[i]/10;
Run Code Online (Sandbox Code Playgroud)
在这里我要求循环从索引1开始并停在倒数第二个值并且也跳过2的值.所以我们可以在LINQ中这样做.IMO我不这么认为但我会很高兴被证明是错的.
我经常在C#代码中看到以下约定:
some_type val;
val = something;
Run Code Online (Sandbox Code Playgroud)
喜欢
DataTable dt;
dt = some_function_returning_datatable();
Run Code Online (Sandbox Code Playgroud)
要么
DataTable dt = new DataTable();
dt = some_function_returning_datatable();
Run Code Online (Sandbox Code Playgroud)
代替
some_type val = something;
DataTable dt = some_function_returning_datatable();
Run Code Online (Sandbox Code Playgroud)
我最初认为这是一个习惯,当你必须在范围顶部声明所有局部变量时.但我已经学会了不要那么快地解雇资深开发者的习惯.
(在我的第3段代码中,当我们dt先分配new然后从函数中分配时,它不会浪费内存)
那么,有一个很好的理由在一行中声明,然后分配吗?
我有许多通过代码动态创建的文本框.
我希望能够为所有文本框分配一个通用事件处理程序,以便更改文本,然后在处理程序中确定哪个文本框已触发事件.
我的代码是:
txtStringProperty.TextChanged += TextBoxValueChanged;
private void TextBoxValueChanged(object sender, RoutedEventArgs e)
{
string propertyName = // I would like the name attribute of the textbox here
}
Run Code Online (Sandbox Code Playgroud)
如果您需要更多信息,请告诉我.
我创建了一个按钮,其命令参数设置和命令使用实现ICommand接口的类.但是我的按钮被禁用了.这是为什么?我从这里得到了这个代码:ICommand就像一块巧克力蛋糕
<Window x:Class="ICommand_Implementation_CSharp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ICommand_Implementation_CSharp"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid>
<Grid.Resources>
<local:HelloWorldCommand x:Key="hwc" />
</Grid.Resources>
<Button Command="{StaticResource hwc}" CommandParameter="Hello"
Height="23" HorizontalAlignment="Left" Margin="212,138,0,0"
Name="Button1" VerticalAlignment="Top" Width="75">Button</Button>
</Grid>
</Grid>
Run Code Online (Sandbox Code Playgroud)
我的班级是
class HelloWorldCommand:ICommand
{
public bool CanExecute(object parameter)
{
return parameter != null;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
MessageBox.Show(parameter.ToString());
}
}
Run Code Online (Sandbox Code Playgroud) 如果您有产品列表,并且每个产品都有一个类别列表.
我如何获得产品使用的食谱清单?
List<Product> products = new List<Product>();
Product product1 = new Product();
product1.Categories.Add("Books");
product1.Categories.Add("Electronics");
Product product2 = new Product();
product2.Categories.Add("Dishes");
product2.Categories.Add("Books");
products.Add( product1 );
products.Add( product2 );
Run Code Online (Sandbox Code Playgroud)
我如何获得"书籍","菜肴","电子产品"列表
我基本上是一个C#家伙,但这些天写的是VB.Net代码.
今天我遇到了一个非常不同的.Net行为
C#代码
enum Color
{
Red,
Green,
Blue
}
class Demo
{
public static void Main()
{
System.Console.WriteLine(Color.Red);
}
}
Run Code Online (Sandbox Code Playgroud)
这打印 Red
但是当这个代码是用VB.Net编写的时候就打印出来了0.
VB.Net代码
Module Module1
Sub Main()
System.Console.WriteLine(Color.Red)
End Sub
End Module
Enum Color
Red
Green
Blue
End Enum
Run Code Online (Sandbox Code Playgroud)
为什么这么不同?