我正在尝试使用下面的代码将字节转换为KB/MB/GB,但是,我似乎无法让它工作.配额的价值是60000000000.
public static double BytesToKilobytes(this Int32 bytes)
{
return bytes / 1000d;
}
public static double BytesToMegabytes(this Int32 bytes)
{
return bytes / 1000d / 1000d;
}
public static double BytesToGigabytes(this Int32 bytes)
{
return bytes / 1000d / 1000d / 1000d;
}
void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
return;
XDocument xDocument = XDocument.Parse(e.Result);
listBox1.ItemsSource = from query in xDocument.Descendants("service")
select new Service
{
type = query.Attribute("type").Value,
id = query.Element("id").Value,
plan = query.Element("username").Value,
quota = query.Element("quota").Value.BytesToGigabytes, …Run Code Online (Sandbox Code Playgroud) 我想ItemsSource将特定元素中的属性用作另一个元素中的绑定之一MultiBinding.这是我到目前为止所拥有的:
<Label>
<Label.Content>
<MultiBinding Converter="{converters:myMultiValueConverter}">
<Binding Path="PageIndex" />
<Binding ElementName="anotherElement" Path="ItemsSource"/>
</MultiBinding>
</Label.Content>
</Label>
Run Code Online (Sandbox Code Playgroud)
这工作一次(当ItemsSource初始设置时),但是当ObservableCollection绑定到原始元素的ItemsSource属性时添加或删除了项目时,绑定无法更新.这种绑定可能吗?
假设我在C#中有这个结构定义:
public struct TimeSlotInfo
{
public int TimeSlotID;
public int StartMin;
public int CalcGridColumn;
public string BackgroundCol;
public bool ToDisable;
}
Run Code Online (Sandbox Code Playgroud)
我有一个linq查询如下:
var TimeSlotsInfo =
from ts in datacon.TimeSlots
select new TimeSlotInfo
{
TimeSlotID = ts.TimeSlotID,
StartMin = ts.StartMin,
CalcGridColumn = CalcTimeSlotGridColumn(ts.StartMin),
BackgroundCol = ts.ColorName,
ToDisable = false
};
Run Code Online (Sandbox Code Playgroud)
如果我设置ListBox的ItemsSource属性如下:
lstBox.ItemsSource = TimeSlotsInfo;
Run Code Online (Sandbox Code Playgroud)
现在,如何设置绑定路径以引用上述查询结果中的"BackgroundCol"字段?
我试过{Binding Path = TimeSlotInfo.BackgroundCol},{Binding Path = TimeSlotInfo/BackgroundCol},最后{Binding Path = BackgroundCol} ...它们似乎都没有工作......
有人可以帮忙吗?我试图尽可能地简化示例.希望我的问题很清楚.提前致谢.
我想在ListView上进行自定义排序,它将DataTable作为ItemsSource:
myListView.ItemsSource = (data as DataTable);
Run Code Online (Sandbox Code Playgroud)
这是我的排序功能的第一行:
DataView view = (myListView.ItemsSource as DataTable).DefaultView;
ListCollectionView coll = (ListCollectionView)CollectionViewSource.GetDefaultView(view);
Run Code Online (Sandbox Code Playgroud)
第二行抛出一个如下的执行:
无法将"System.Windows.Data.BindingListCollectionView"强制转换为"System.Windows.Data.ListCollectionView"
有人有解决方案吗?这4个答案
我不知道我在这里做错了什么.我有一个ListBox,其DataContext与ItemsSource设置,但并没有什么的ListBox,当我跑我的应用程序.在调试时,我的方法的第一行获取ListBox永远不会被击中的项目.这就是我所拥有的:
// Constructor in UserControl
public TemplateList()
{
_templates = new Templates();
InitializeComponent();
DataContext = this;
}
// ItemsSource of ListBox
public List<Template> GetTemplates()
{
if (!tryReadTemplatesIfNecessary(ref _templates))
{
return new List<Template>
{
// Template with Name property set:
new Template("No saved templates", null)
};
}
return _templates.ToList();
}
Run Code Online (Sandbox Code Playgroud)
这是我的XAML:
<ListBox ItemsSource="{Binding Path=GetTemplates}" Grid.Row="1" Grid.Column="1"
Width="400" Height="300" DisplayMemberPath="Name"
SelectedValuePath="Name"/>
Run Code Online (Sandbox Code Playgroud)
在Template类的一个实例上,有一个Name属性只是一个string.我想要的只是显示模板名称列表.用户不会更改a中的任何数据Template,ListBox只需要是只读的. …
我有一个UserControl,我写的是从自定义对象中显示一些属性.这些对象有多个实例,因此我有一个ObservableCollection,所以我可以将它们设置为与ListView的ItemsSource绑定.现在,我可以在ListView中为我的类的每个实例显示此UserControl的实例.
问题是我真的不想要ListView的行为.我不希望用户能够选择整个UserControl.实际上,用户应该能够在UserControl中选择单个元素.
我想过只使用StackPanel来放入这些UserControl,但它没有ItemesSource属性.是否有一种简单的方法可以实现这一目标?
我无法将组合框的ItemsSource设置为数组.我已经尝试将DataContext设置为找到Array的类,然后在XAML中设置绑定
class Car
{
public string[] makes;
}
Run Code Online (Sandbox Code Playgroud)
...
public MainWindow()
{
Car _Car = new Car();
_Car.makes = new string[]
{
"Toyota",
"Mitsubishi",
"Audi",
"BMW"
};
this.DataContext = _Car;
}
Run Code Online (Sandbox Code Playgroud)
然后在XAML中
<ComboBox Name="cars" Grid.Column="0"
Grid.Row="0" Margin="5"
ItemsSource="{Binding Path=makes}"/>
Run Code Online (Sandbox Code Playgroud)
它似乎没有做任何事情.我的汽车组合框不会有任何物品.
我也试过明确分配
cars.ItemsSource= new string[]{
"Toyota",
"Mitsubishi",
"Audi",
"BMW"
};
Run Code Online (Sandbox Code Playgroud)
但后来我收到此错误消息:
调用的目标抛出了异常.
我错过了什么吗?
考虑以下XAML:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:WpfApplication1"
DataContext="{Binding Source={x:Static c:ViewModel.Instance}}"
>
<Grid>
<DataGrid DataContext="{Binding ItemsViewSource}" ItemsSource="{Binding}" />
</Grid>
Run Code Online (Sandbox Code Playgroud)
和视图模型:
public class ItemViewModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class ViewModel
{
public static ViewModel Instance { get; set; }
static ViewModel()
{
Instance = new ViewModel();
}
public ObservableCollection<ItemViewModel> Items { get; private set; }
public CollectionViewSource ItemsViewSource { get; private set; }
public ViewModel()
{
Items = new ObservableCollection<ItemViewModel>(); …Run Code Online (Sandbox Code Playgroud) 如何对列表框内容进行排序?在我看来,将它保留在UI层更有意义,因为排序不会影响我的业务逻辑,所以它可能在xaml或代码隐藏中.我无法弄明白该怎么做.
我有一个ItemsCountrol,它的ItemsSource属性绑定到ObservableCollection。我有一个显示此类型的用户控件(TeamUserControl)。我创建了一个数据模板,用于为itemssource集合中的每个customtype项加载此用户控件。此时,我在TeamUserControl中所做的任何Binding语句都可以直接通过路径{Binding Path = TeamOwner}引用Team属性并开始工作。有没有一种方法可以将引用绑定到用户控件代表的ItemsSource项目?例如,在TeamUserControl中创建Team类型的依赖项属性,并将其绑定到ItemsSource中的项目实例。
<ItemsControl Name="ItemCtrl" Grid.Row="0" ItemsSource="{Binding Path=League.Teams}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<mycontrols:TeamUserControl AttachedTeam="{Binding ???}" TeamOwnerName="{Binding Path=TeamOwner}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
在此示例中,窗口表示具有属性:ObservableCollection Teams的“联盟”类。并且有一个类“ Team”,其属性为:TeamOwner。TeamUserControl具有两个从属属性:Team类型的AttachedTeam和string类型的TeamOwnerName。
我包括了team-owner属性引用,以显示每个这些用户控件都有一个Team实例。我只是不确定如何引用它。
itemssource ×10
wpf ×8
c# ×5
binding ×4
xaml ×3
combobox ×1
data-binding ×1
datacontext ×1
datagrid ×1
datatable ×1
datatemplate ×1
itemtemplate ×1
linq ×1
listbox ×1
path ×1
sorting ×1