我们可以使用[Windows.Markup.XamlReader] :: Load在PowerShell中加载XAML文件,但有没有办法将某些PowerShell脚本指定为代码?
我在代码隐藏中生成一个下拉列表,无法自动触发selectedindexchanged事件.它直接放入ASPX页面时工作正常,但我需要它在代码隐藏中.
这不起作用:
var deptList = new DropDownList
{
ID = "deptList",
DataSource = departments,
DataTextField = "deptname",
DataValueField = "deptid",
AutoPostBack = true,
EnableViewState = true
};
deptList.SelectedIndexChanged += new EventHandler(deptList_SelectedIndexChanged);
deptList.DataSource = departments;
deptList.DataTextField = "deptname";
deptList.DataValueField = "deptid";
if (!IsPostBack)
deptList.DataBind();
deptList.Items.Insert(0, new ListItem("---Select Department---", string.Empty));
writer.Write("Select a department: ");
deptList.RenderControl(writer);
Run Code Online (Sandbox Code Playgroud)
但这有效:
<asp:DropDownList ID="deptList" AutoPostBack="true" runat="server" OnSelectedIndexChanged="deptList_SelectedIndexChanged"></asp:DropDownList>
Run Code Online (Sandbox Code Playgroud) 如何通过C#代码隐藏向我的DetailsView对象添加带有复选框的模板字段?我在阅读asp.net代码时遇到了麻烦.
我已经使用分配给属性的值实例化了TemplateField和CheckBox对象.但是当我使用Fields.Add()时,复选框不显示.
TemplateField tf_ForMalls = new TemplateField();
tf_ForMalls.HeaderText = "For Malls";
CheckBox chk_ForMalls = new CheckBox();
chk_ForMalls.ID = "chkDelete";
tf_ForMalls.ItemTemplate = chk_ForMalls as ITemplate;
dv_SpotDetail.Fields.Add(tf_ForMalls);
Run Code Online (Sandbox Code Playgroud) 我正在尝试在代码隐藏中创建DataTemplate.我的DataTrigger存在问题.
这是DataTemplate,用xaml编写:
<DataTemplate x:Key="XamlTemplate" >
<TextBox Text="{Binding Name}" Name="element" Width="100"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Flag}" Value="true">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="element" Storyboard.TargetProperty="Width"
To="200" Duration="0:0:2" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
这是我用c#写的
var template = new DataTemplate();
//create visual tree
var textFactory = new FrameworkElementFactory(typeof(TextBox));
textFactory.SetBinding(TextBox.TextProperty, new Binding("Name"));
textFactory.SetValue(TextBox.NameProperty, "element");
textFactory.SetValue(TextBox.WidthProperty, 100D);
template.VisualTree = textFactory;
//create trigger
var animation = new DoubleAnimation();
animation.To = 200;
animation.Duration = TimeSpan.FromSeconds(2);
Storyboard.SetTargetProperty(animation, new PropertyPath("Width"));
Storyboard.SetTargetName(animation, "element");
var storyboard = new …Run Code Online (Sandbox Code Playgroud) 情况:
我有一个UserControl像这样的"包装器面板" (为简洁起见,删除了名称空间和视觉细节):
<UserControl ...>
<Grid x:Name="LayoutRoot" Background="White">
<ContentPresenter x:Name="integratedPanelContent" Margin="5" />
</Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
然后在Code-behind中我注册了一个依赖属性
public FrameworkElement PanelContent
{
get { return (FrameworkElement)GetValue(PanelContentProperty); }
set { SetValue(PanelContentProperty, value); }
}
public static readonly DependencyProperty PanelContentProperty =
DependencyProperty.Register("PanelContent", typeof(FrameworkElement), typeof(MyWrapperPanel),
new PropertyMetadata(null, OnPanelContentChanged));
private static void OnPanelContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((MyWrapperPanel)d).OnSetContentChanged(e);
}
protected virtual void OnSetContentChanged(DependencyPropertyChangedEventArgs e)
{
if (PanelContent != null)
integratedPanelContent.Content = PanelContent;
}
Run Code Online (Sandbox Code Playgroud)
现在我可以将任何内容包装到我的控件中:
<my:MyWrapperPanel x:Name="myWrap">
<my:MyWrapperPanel.PanelContent>
<TextBlock x:Name="tbxNothing" Text="Nothing" />
</my:MyWrapperPanel.PanelContent>
</my:MyWrapperPanel>
Run Code Online (Sandbox Code Playgroud)
问题描述:
每当我尝试在代码隐藏中使用引用tbxNothing时,系统抛出, …
我有一个用户控件.我有时会再遇到这种情况,但可以通过使用" New()contructor" 来解决它.但是我仍然想知道我做错了什么因为如果加载了控件就必须触发load事件!
这是一些代码:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:OUTPUT___VideoContent"
Title="OUTPUT - VideoContent" Height="350" Width="525" Icon="/OUTPUT%20-%20VideoContent;component/Images/VideoContent.png">
<Grid x:Name="LayoutRoot">
<Grid x:Name="VideoGrid">
<my:ucVideoPresenter x:Name="VideoPresenter1"/>
<TextBlock x:Name="txtInfo" Visibility="Collapsed" />
</Grid>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
在usercontrol中,load事件在WPF上声明或代码发送没有任何成功!


我正在使用JavaScript,它具有以下代码:
<script type="text/javascript">
var count = 0;
jQuery('td').click(function () {
if ($(this).hasClass('process')) {
count = count+100;
alert('count');
}
});
</script>
Run Code Online (Sandbox Code Playgroud)
因此,如果点击该值加100,我使用警报检查,现在如何访问count我的代码隐藏中的var
关于在后面的代码中设置图像源有很多问题和答案,例如在代码中设置WPF图像源.
我已经遵循了所有这些步骤,但却无法设置图像.我在VS2010中使用WPF C#进行编码.我将所有图像文件放在名为" Images " 的文件夹下,所有图像文件都设置为Copy always.按照文档中的说明," 构建操作"设置为" 资源".
我的代码如下.我在XAML中设置了一个dog.png并在后面的代码中将其更改为cat.png.
// my XAML
<Image x:Name="imgAnimal" Source="Images/dog.png" />
// my C#
BitmapImage img = new BitmapImage();
img.UriSource = new Uri(@"pack://application:,,,/FooApplication;component/Images/cat.png");
imgAnimal.Source = img;
Run Code Online (Sandbox Code Playgroud)
然后我得到一张空虚的空白图像.我不明白为什么.NET会使图像设置如此复杂......
[编辑]
以下是有效的
imgAnimal.Source = new BitmapImage(new Uri(@"pack://application:,,,/FooApplication;component/Images/cat.png"));
Run Code Online (Sandbox Code Playgroud)
它有效,但我看不出这两个代码有什么区别.为什么早期不起作用而后者呢?对我来说他们是一样的..
我想复制一个对象列表,但我不断获得对象之间的引用.
List<MyClass> copy = original;
Run Code Online (Sandbox Code Playgroud)
当我更改副本列表中的值时,原始列表也会被修改.有办法解决它吗?
我的代码隐藏中有这一行:
lblAboutMe.Text = (DT1["UserBody"].ToString());
Run Code Online (Sandbox Code Playgroud)
没问题.但是现在,我们只想显示段落的开头,然后是省略号.所以,而不是:
请仔细阅读,因为我讨厌与没有这样做的人浪费时间.如果你有穆斯林的问题,请继续,因为我没有.我曾经练习伊斯兰教,虽然我不再这样做,但我仍然尊重它并且厌恶人们通过追随媒体或耻辱而不是体验它或与穆斯林交谈来教育自己的无知.参考本篇文章后面的无药政策,是的,罐装/大麻算是一种毒品,对我来说不是,所以请继续.我知道接下来的事情让我看起来很冷,但我真的非常热情和热爱,非常忠诚于正确的人,我只是厌倦了被扮演并被视为理所当然/有利.人们撒谎太多,忽视了我说的我不想要的东西.我在这里多次被告知我所寻求的是太多了.
我们想要第一个,比方说100个字符,并用省略号跟随它.所以,像:
请仔细阅读,因为我讨厌与没有这样做的人浪费时间.如果你有穆斯林的问题,请继续,因为我没有.我曾经练习伊斯兰教,虽然我不再这样做但我仍然尊重它,并且厌恶人们通过追随媒体或耻辱而不是经历它或与穆斯林交谈来教育自己而表现出来的无知......
我们怎么能在代码隐藏中做到这一点?我觉得这很容易(因为在Access中很容易),但我还是这种语言的新手.
code-behind ×10
asp.net ×5
c# ×4
wpf ×4
datatemplate ×1
datatrigger ×1
events ×1
javascript ×1
load ×1
powershell ×1
silverlight ×1
variables ×1
wrapper ×1
xaml ×1